github.com/0xsequence/ethkit@v1.25.0/go-ethereum/accounts/url_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package accounts 18 19 import ( 20 "testing" 21 ) 22 23 func TestURLParsing(t *testing.T) { 24 url, err := parseURL("https://ethereum.org") 25 if err != nil { 26 t.Errorf("unexpected error: %v", err) 27 } 28 if url.Scheme != "https" { 29 t.Errorf("expected: %v, got: %v", "https", url.Scheme) 30 } 31 if url.Path != "ethereum.org" { 32 t.Errorf("expected: %v, got: %v", "ethereum.org", url.Path) 33 } 34 35 for _, u := range []string{"ethereum.org", ""} { 36 if _, err = parseURL(u); err == nil { 37 t.Errorf("input %v, expected err, got: nil", u) 38 } 39 } 40 } 41 42 func TestURLString(t *testing.T) { 43 url := URL{Scheme: "https", Path: "ethereum.org"} 44 if url.String() != "https://ethereum.org" { 45 t.Errorf("expected: %v, got: %v", "https://ethereum.org", url.String()) 46 } 47 48 url = URL{Scheme: "", Path: "ethereum.org"} 49 if url.String() != "ethereum.org" { 50 t.Errorf("expected: %v, got: %v", "ethereum.org", url.String()) 51 } 52 } 53 54 func TestURLMarshalJSON(t *testing.T) { 55 url := URL{Scheme: "https", Path: "ethereum.org"} 56 json, err := url.MarshalJSON() 57 if err != nil { 58 t.Errorf("unexpcted error: %v", err) 59 } 60 if string(json) != "\"https://ethereum.org\"" { 61 t.Errorf("expected: %v, got: %v", "\"https://ethereum.org\"", string(json)) 62 } 63 } 64 65 func TestURLUnmarshalJSON(t *testing.T) { 66 url := &URL{} 67 err := url.UnmarshalJSON([]byte("\"https://ethereum.org\"")) 68 if err != nil { 69 t.Errorf("unexpcted error: %v", err) 70 } 71 if url.Scheme != "https" { 72 t.Errorf("expected: %v, got: %v", "https", url.Scheme) 73 } 74 if url.Path != "ethereum.org" { 75 t.Errorf("expected: %v, got: %v", "https", url.Path) 76 } 77 } 78 79 func TestURLComparison(t *testing.T) { 80 tests := []struct { 81 urlA URL 82 urlB URL 83 expect int 84 }{ 85 {URL{"https", "ethereum.org"}, URL{"https", "ethereum.org"}, 0}, 86 {URL{"http", "ethereum.org"}, URL{"https", "ethereum.org"}, -1}, 87 {URL{"https", "ethereum.org/a"}, URL{"https", "ethereum.org"}, 1}, 88 {URL{"https", "abc.org"}, URL{"https", "ethereum.org"}, -1}, 89 } 90 91 for i, tt := range tests { 92 result := tt.urlA.Cmp(tt.urlB) 93 if result != tt.expect { 94 t.Errorf("test %d: cmp mismatch: expected: %d, got: %d", i, tt.expect, result) 95 } 96 } 97 }