github.com/codingfuture/orig-energi3@v0.8.4/accounts/url_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package accounts
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  func TestURLParsing(t *testing.T) {
    25  	url, err := parseURL("https://ethereum.org")
    26  	if err != nil {
    27  		t.Errorf("unexpected error: %v", err)
    28  	}
    29  	if url.Scheme != "https" {
    30  		t.Errorf("expected: %v, got: %v", "https", url.Scheme)
    31  	}
    32  	if url.Path != "ethereum.org" {
    33  		t.Errorf("expected: %v, got: %v", "ethereum.org", url.Path)
    34  	}
    35  
    36  	_, err = parseURL("ethereum.org")
    37  	if err == nil {
    38  		t.Error("expected err, got: nil")
    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  }