<commit_before>// Copyright 2016 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package upspin

import (
	"strings"
	"testing"
)

func TestParseAndString(t *testing.T) {
	assertParsesAndEncodes(t, "gcp,localhost:8080")
	assertParsesAndEncodes(t, "remote,localhost:8080")
	assertParsesAndEncodes(t, "https,https://localhost:8080")
	assertParsesAndEncodes(t, "inprocess")
}

func TestErrorCases(t *testing.T) {
	assertError(t, "remote", "requires a netaddr")
	assertError(t, "supersonic,https://supersonic.com", "unknown transport type")
	assertError(t, "gcp", "requires a netaddr")
	assertError(t, "https", "requires a netaddr")
}

// Test printing of an erroneous endpoint. Mostly to protect
// against an error found by vet and fixed.
func TestErroneousString(t *testing.T) {
	e := Endpoint{Transport: 127, NetAddr: "whatnot"}
	const expect = "unknown transport {transport(127), whatnot}"
	got := e.String()
	if got != expect {
		t.Fatalf("expected %q; got %q", expect, got)
	}
}

func TestJSON(t *testing.T) {
	e := Endpoint{Transport: GCP, NetAddr: "whatnot"}
	buf, err := e.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	newE := new(Endpoint)
	err = newE.UnmarshalJSON(buf)
	if err != nil {
		t.Fatal(err)
	}
	if e != *newE {
		t.Errorf("Expected %q, got %q", e, newE)
	}
}

func assertError(t *testing.T, epString string, substringError string) {
	_, err := ParseEndpoint(epString)
	if err == nil {
		t.Fatal("Expected error")
	}
	if !strings.Contains(err.Error(), substringError) {
		t.Errorf("Expected error prefix %q, got %q", substringError, err)
	}
}

func assertParsesAndEncodes(t *testing.T, epString string) {
	ep, err := ParseEndpoint(epString)
	if err != nil {
		t.Fatal(err)
	}
	retStr := ep.String()
	if retStr != epString {
		t.Errorf("Expected %s, got %s", epString, retStr)
	}
}<commit_msg>upspin: remove asserts from endpoint test<commit_after>// Copyright 2016 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package upspin

import (
	"strings"
	"testing"
)

func TestParseAndString(t *testing.T) {
	tests := []string{
		"gcp,localhost:8080",
		"remote,localhost:8080",
		"https,https://localhost:8080",
		"inprocess",
	}
	for _, test := range tests {
		ep, err := ParseEndpoint(test)
		if err != nil {
			t.Errorf("parsing %q: %v", test, err)
			continue
		}
		got := ep.String()
		if got != test {
			t.Errorf("got %q, want %q", got, test)
		}
	}
}

func TestErrorCases(t *testing.T) {
	tests := []struct {
		endpoint, error string
	}{
		{"remote", "requires a netaddr"},
		{"supersonic,https://supersonic.com", "unknown transport type"},
		{"gcp", "requires a netaddr"},
		{"https", "requires a netaddr"},
	}
	for _, test := range tests {
		_, err := ParseEndpoint(test.endpoint)
		if err == nil {
			t.Errorf("expected error for %q", test.endpoint)
			continue
		}
		if !strings.Contains(err.Error(), test.error) {
			t.Errorf("got error %q, expected %q", err, test.error)
		}
	}
}

// Test printing of an erroneous endpoint. Mostly to protect
// against an error found by vet and fixed.
func TestErroneousString(t *testing.T) {
	e := Endpoint{Transport: 127, NetAddr: "whatnot"}
	const expect = "unknown transport {transport(127), whatnot}"
	got := e.String()
	if got != expect {
		t.Fatalf("expected %q; got %q", expect, got)
	}
}

func TestJSON(t *testing.T) {
	e := Endpoint{Transport: GCP, NetAddr: "whatnot"}
	buf, err := e.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	newE := new(Endpoint)
	err = newE.UnmarshalJSON(buf)
	if err != nil {
		t.Fatal(err)
	}
	if e != *newE {
		t.Errorf("Expected %q, got %q", e, newE)
	}
}
{'fraction_non_alphanumeric': 0.15486961149547632, 'fraction_numerical': 0.011708355508249068, 'mean_word_length': 4.0728744939271255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '627179', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1341, 'n_words': 386}