File: encoder_test.go

package info (click to toggle)
golang-github-multiformats-go-multibase 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 216 kB
  • sloc: makefile: 7
file content (53 lines) | stat: -rw-r--r-- 1,155 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package multibase

import (
	"testing"
	"unicode/utf8"
)

func TestInvalidCode(t *testing.T) {
	_, err := NewEncoder('q')
	if err == nil {
		t.Error("expected failure")
	}
}

func TestInvalidName(t *testing.T) {
	values := []string{"invalid", "", "q"}
	for _, val := range values {
		_, err := EncoderByName(val)
		if err == nil {
			t.Errorf("EncoderByName(%v) expected failure", val)
		}
	}
}

func TestEncoder(t *testing.T) {
	for name, code := range Encodings {
		encoder, err := NewEncoder(code)
		if err != nil {
			t.Fatal(err)
		}
		// Make sure the MustNewEncoder doesn't panic
		MustNewEncoder(code)
		str, err := Encode(code, sampleBytes)
		if err != nil {
			t.Fatal(err)
		}
		str2 := encoder.Encode(sampleBytes)
		if str != str2 {
			t.Errorf("encoded string mismatch: %s != %s", str, str2)
		}
		_, err = EncoderByName(name)
		if err != nil {
			t.Fatalf("EncoderByName(%s) failed: %v", name, err)
		}
		// Test that an encoder can be created from the single letter
		// prefix
		r, _ := utf8.DecodeRuneInString(str)
		_, err = EncoderByName(string(r))
		if err != nil {
			t.Fatalf("EncoderByName(%s) failed: %v", string(r), err)
		}
	}
}