-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
427e79e
commit 1b47dd2
Showing
8 changed files
with
164 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package output | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/sundowndev/phoneinfoga/v2/lib/remote" | ||
"github.com/sundowndev/phoneinfoga/v2/test/goldenfile" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestConsoleOutput(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
dirName string | ||
result map[string]interface{} | ||
errs map[string]error | ||
wantErr error | ||
}{ | ||
{ | ||
name: "should produce empty output", | ||
dirName: "testdata/console_empty.txt", | ||
result: map[string]interface{}{}, | ||
errs: map[string]error{}, | ||
}, | ||
{ | ||
name: "should produce valid output", | ||
dirName: "testdata/console_valid.txt", | ||
result: map[string]interface{}{ | ||
"numverify": remote.NumverifyScannerResponse{ | ||
Valid: true, | ||
Number: "test", | ||
LocalFormat: "test", | ||
InternationalFormat: "test", | ||
CountryPrefix: "test", | ||
CountryCode: "test", | ||
CountryName: "test", | ||
Location: "test", | ||
Carrier: "test", | ||
LineType: "test", | ||
}, | ||
}, | ||
errs: map[string]error{}, | ||
}, | ||
{ | ||
name: "should produce valid output with errors", | ||
dirName: "testdata/console_valid_with_errors.txt", | ||
result: map[string]interface{}{ | ||
"testscanner": nil, | ||
"numverify": remote.NumverifyScannerResponse{ | ||
Valid: true, | ||
Number: "test", | ||
LocalFormat: "test", | ||
InternationalFormat: "test", | ||
CountryPrefix: "test", | ||
CountryCode: "test", | ||
CountryName: "test", | ||
Location: "test", | ||
Carrier: "test", | ||
LineType: "test", | ||
}, | ||
}, | ||
errs: map[string]error{ | ||
"googlesearch": errors.New("dummy error"), | ||
"fakescanner": errors.New("dummy error 2"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range testcases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
shouldUpdate := tt.dirName == *goldenfile.Update | ||
|
||
expected, err := os.ReadFile(tt.dirName) | ||
if err != nil && !shouldUpdate { | ||
t.Fatal(err) | ||
} | ||
|
||
got := new(bytes.Buffer) | ||
err = GetOutput(Console, got).Write(tt.result, tt.errs) | ||
if tt.wantErr != nil { | ||
assert.EqualError(t, err, tt.wantErr.Error()) | ||
} else { | ||
assert.Nil(t, err) | ||
} | ||
|
||
if shouldUpdate { | ||
err = os.WriteFile(tt.dirName, got.Bytes(), 0644) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected, err = os.ReadFile(tt.dirName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
assert.Equal(t, string(expected), got.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 scanner(s) succeeded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Results for numverify | ||
Valid: true | ||
Number: test | ||
Local format: test | ||
International format: test | ||
Country prefix: test | ||
Country code: test | ||
Country name: test | ||
Location: test | ||
Carrier: test | ||
Line type: test | ||
|
||
1 scanner(s) succeeded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Results for numverify | ||
Valid: true | ||
Number: test | ||
Local format: test | ||
International format: test | ||
Country prefix: test | ||
Country code: test | ||
Country name: test | ||
Location: test | ||
Carrier: test | ||
Line type: test | ||
|
||
The following scanners returned errors: | ||
googlesearch: dummy error | ||
fakescanner: dummy error 2 | ||
|
||
2 scanner(s) succeeded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package goldenfile | ||
|
||
import ( | ||
"flag" | ||
) | ||
|
||
var Update = flag.String("update", "", "name of test to update") |