gitlab.com/flarenetwork/coreth@v0.1.1/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 _, err = parseURL("ethereum.org") 46 if err == nil { 47 t.Error("expected err, got: nil") 48 } 49 } 50 51 func TestURLString(t *testing.T) { 52 url := URL{Scheme: "https", Path: "ethereum.org"} 53 if url.String() != "https://ethereum.org" { 54 t.Errorf("expected: %v, got: %v", "https://ethereum.org", url.String()) 55 } 56 57 url = URL{Scheme: "", Path: "ethereum.org"} 58 if url.String() != "ethereum.org" { 59 t.Errorf("expected: %v, got: %v", "ethereum.org", url.String()) 60 } 61 } 62 63 func TestURLMarshalJSON(t *testing.T) { 64 url := URL{Scheme: "https", Path: "ethereum.org"} 65 json, err := url.MarshalJSON() 66 if err != nil { 67 t.Errorf("unexpcted error: %v", err) 68 } 69 if string(json) != "\"https://ethereum.org\"" { 70 t.Errorf("expected: %v, got: %v", "\"https://ethereum.org\"", string(json)) 71 } 72 } 73 74 func TestURLUnmarshalJSON(t *testing.T) { 75 url := &URL{} 76 err := url.UnmarshalJSON([]byte("\"https://ethereum.org\"")) 77 if err != nil { 78 t.Errorf("unexpcted error: %v", err) 79 } 80 if url.Scheme != "https" { 81 t.Errorf("expected: %v, got: %v", "https", url.Scheme) 82 } 83 if url.Path != "ethereum.org" { 84 t.Errorf("expected: %v, got: %v", "https", url.Path) 85 } 86 } 87 88 func TestURLComparison(t *testing.T) { 89 tests := []struct { 90 urlA URL 91 urlB URL 92 expect int 93 }{ 94 {URL{"https", "ethereum.org"}, URL{"https", "ethereum.org"}, 0}, 95 {URL{"http", "ethereum.org"}, URL{"https", "ethereum.org"}, -1}, 96 {URL{"https", "ethereum.org/a"}, URL{"https", "ethereum.org"}, 1}, 97 {URL{"https", "abc.org"}, URL{"https", "ethereum.org"}, -1}, 98 } 99 100 for i, tt := range tests { 101 result := tt.urlA.Cmp(tt.urlB) 102 if result != tt.expect { 103 t.Errorf("test %d: cmp mismatch: expected: %d, got: %d", i, tt.expect, result) 104 } 105 } 106 }