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