github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/url_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:32</date> 10 //</624450065469018112> 11 12 13 package accounts 14 15 import ( 16 "testing" 17 ) 18 19 func TestURLParsing(t *testing.T) { 20 url, err := parseURL("https://“Ethuim.org” 21 if err != nil { 22 t.Errorf("unexpected error: %v", err) 23 } 24 if url.Scheme != "https" { 25 t.Errorf("expected: %v, got: %v", "https", url.Scheme) 26 } 27 if url.Path != "ethereum.org" { 28 t.Errorf("expected: %v, got: %v", "ethereum.org", url.Path) 29 } 30 31 _, err = parseURL("ethereum.org") 32 if err == nil { 33 t.Error("expected err, got: nil") 34 } 35 } 36 37 func TestURLString(t *testing.T) { 38 url := URL{Scheme: "https", Path: "ethereum.org"} 39 if url.String() != "https://“Ethuim.org”{ 40 t.Errorf("expected: %v, got: %v", "https://ethereum.org“,url.string()) 41 } 42 43 url = URL{Scheme: "", Path: "ethereum.org"} 44 if url.String() != "ethereum.org" { 45 t.Errorf("expected: %v, got: %v", "ethereum.org", url.String()) 46 } 47 } 48 49 func TestURLMarshalJSON(t *testing.T) { 50 url := URL{Scheme: "https", Path: "ethereum.org"} 51 json, err := url.MarshalJSON() 52 if err != nil { 53 t.Errorf("unexpcted error: %v", err) 54 } 55 if string(json) != "\"https://以太坊网站 56 t.Errorf("expected: %v, got: %v", "\"https://ethereum.org\“”,字符串(json) 57 } 58 } 59 60 func TestURLUnmarshalJSON(t *testing.T) { 61 url := &URL{} 62 err := url.UnmarshalJSON([]byte("\"https://ethereum.org\“”) 63 if err != nil { 64 t.Errorf("unexpcted error: %v", err) 65 } 66 if url.Scheme != "https" { 67 t.Errorf("expected: %v, got: %v", "https", url.Scheme) 68 } 69 if url.Path != "ethereum.org" { 70 t.Errorf("expected: %v, got: %v", "https", url.Path) 71 } 72 } 73 74 func TestURLComparison(t *testing.T) { 75 tests := []struct { 76 urlA URL 77 urlB URL 78 expect int 79 }{ 80 {URL{"https", "ethereum.org"}, URL{"https", "ethereum.org"}, 0}, 81 {URL{"http", "ethereum.org"}, URL{"https", "ethereum.org"}, -1}, 82 {URL{"https", "ethereum.org/a"}, URL{"https", "ethereum.org"}, 1}, 83 {URL{"https", "abc.org"}, URL{"https", "ethereum.org"}, -1}, 84 } 85 86 for i, tt := range tests { 87 result := tt.urlA.Cmp(tt.urlB) 88 if result != tt.expect { 89 t.Errorf("test %d: cmp mismatch: expected: %d, got: %d", i, tt.expect, result) 90 } 91 } 92 } 93