mirror of
				https://github.com/TomWright/dasel.git
				synced 2022-05-22 02:32:45 +03:00 
			
		
		
		
	Add tests for "NewFromFile"
This commit is contained in:
		
							
								
								
									
										55
									
								
								node_test.go
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								node_test.go
									
									
									
									
									
								
							| @@ -4,10 +4,11 @@ import ( | ||||
| 	"encoding/json" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"github.com/tomwright/dasel" | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/tomwright/dasel" | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| ) | ||||
|  | ||||
| // ExampleNode_ReadmeExample tests the code from the readme explanation. | ||||
| @@ -1312,3 +1313,53 @@ func TestNode_DeleteMultiple_Query(t *testing.T) { | ||||
| 	}), ".", []interface{}{})) | ||||
| 	t.Run("RootNodeUnknown", deleteMultipleTest(dasel.New(false), ".", map[string]interface{}{})) | ||||
| } | ||||
|  | ||||
| func TestNodeNewFromFile(t *testing.T) { | ||||
| 	// Not sure what to test here: | ||||
| 	// should all file/parser tests be duplicated? | ||||
| 	// E.g.: TestLoadFromFile, TestNewReadParserFromString | ||||
|  | ||||
| 	tests := []struct { | ||||
| 		Name   string | ||||
| 		File   string | ||||
| 		Parser string | ||||
| 		Err    error | ||||
| 	}{ | ||||
| 		{ | ||||
| 			Name: "File exists and valid parser specified", | ||||
| 			File: "./tests/assets/example.json", Parser: "json", Err: nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name: "File exists and invalid parser specified", | ||||
| 			File: "./tests/assets/example.json", Parser: "bad", Err: storage.UnknownParserErr{Parser: "bad"}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name: "File not exists and valid parser specified", | ||||
| 			File: "no_such_file", Parser: "json", Err: errors.New("could not open file: open no_such_file: The system cannot find the file specified."), | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name: "File not exists and invalid parser specified", | ||||
| 			File: "no_such_file", Parser: "bad", Err: storage.UnknownParserErr{Parser: "bad"}, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	for _, testCase := range tests { | ||||
| 		tc := testCase | ||||
| 		t.Run(tc.Name, func(t *testing.T) { | ||||
| 			_, err := dasel.NewFromFile(tc.File, tc.Parser) | ||||
|  | ||||
| 			if tc.Err == nil && err != nil { | ||||
| 				t.Errorf("expected err %v, got %v", tc.Err, err) | ||||
| 				return | ||||
| 			} | ||||
| 			if tc.Err != nil && err == nil { | ||||
| 				t.Errorf("expected err %v, got %v", tc.Err, err) | ||||
| 				return | ||||
| 			} | ||||
| 			if tc.Err != nil && err != nil && err.Error() != tc.Err.Error() { | ||||
| 				t.Errorf("expected err %v, got %v", tc.Err, err) | ||||
| 				return | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 beatcracker
					beatcracker