github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/mail/utils_test.go (about) 1 package mail 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test/count" 7 ) 8 9 type testGetDomainT struct { 10 Email string 11 Expected string 12 Error bool 13 } 14 15 func TestGetDomain(t *testing.T) { 16 tests := []testGetDomainT{ 17 { 18 Email: `foo@bar`, 19 Expected: `bar`, 20 }, 21 { 22 Email: `foo@bar.com`, 23 Expected: `bar.com`, 24 }, 25 { 26 Email: `foo@192.168.1.1`, 27 Expected: `192.168.1.1`, 28 }, 29 { 30 Email: `@foo`, 31 Expected: `foo`, 32 Error: false, 33 }, 34 { 35 Email: `foo@`, 36 Expected: ``, 37 Error: true, 38 }, 39 { 40 Email: `foo`, 41 Expected: ``, 42 Error: true, 43 }, 44 } 45 46 count.Tests(t, len(tests)) 47 48 for i, test := range tests { 49 actual, err := getDomain(test.Email) 50 51 if (err == nil) == test.Error { 52 t.Errorf("Error mismatch on test %d:", i) 53 t.Logf(" Email: '%s'", test.Email) 54 t.Logf(" Expected: '%s'", test.Expected) 55 t.Logf(" Actual: '%s'", actual) 56 t.Logf(" err exp: %v", test.Error) 57 t.Logf(" err act: %v", err) 58 } 59 60 if test.Expected != actual { 61 t.Errorf("domain does not match expected on test %d:", i) 62 t.Logf(" Email: '%s'", test.Email) 63 t.Logf(" Expected: '%s'", test.Expected) 64 t.Logf(" Actual: '%s'", actual) 65 t.Logf(" err exp: %v", test.Error) 66 t.Logf(" err act: %v", err) 67 } 68 } 69 }