github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/decode/string_test.go (about)

     1  package decode
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     7  )
     8  
     9  func TestArticulateString(t *testing.T) {
    10  	type args struct {
    11  		hex string
    12  	}
    13  	tests := []struct {
    14  		name        string
    15  		args        args
    16  		wantResult  string
    17  		wantSuccess bool
    18  	}{
    19  		{
    20  			name: "CID-like",
    21  			args: args{
    22  				hex: "0x516d51344e3338384c736e77384e486e4a72526f59327561676d5071736d3353",
    23  			},
    24  			wantResult:  "QmQ4N388Lsnw8NHnJrRoY2uagmPqsm3S",
    25  			wantSuccess: true,
    26  		},
    27  		{
    28  			name: "not a string",
    29  			args: args{
    30  				hex: "0xf31b5730bd294639c67fa143fca1dad2c7aa05644f8d8b0e104aad5922d2f866",
    31  			},
    32  			wantResult:  "",
    33  			wantSuccess: false,
    34  		},
    35  		{
    36  			name: "string with newline",
    37  			args: args{
    38  				hex: "0x" + base.Bytes2Hex([]byte("Line1\nLine2")),
    39  			},
    40  			wantResult:  "Line1[n]Line2",
    41  			wantSuccess: true,
    42  		},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			gotResult, gotPureStr := ArticulateString(tt.args.hex)
    47  			if gotResult != tt.wantResult {
    48  				t.Errorf("ArticulateString() gotResult = %v, want %v", gotResult, tt.wantResult)
    49  			}
    50  			if gotPureStr != tt.wantSuccess {
    51  				t.Errorf("ArticulateString() gotSuccess = %v, want %v", gotPureStr, tt.wantSuccess)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestArticulateEncodedString(t *testing.T) {
    58  	type args struct {
    59  		hex string
    60  	}
    61  	tests := []struct {
    62  		name       string
    63  		args       args
    64  		wantResult string
    65  		wantErr    bool
    66  	}{
    67  		{
    68  			name: "valid string",
    69  			args: args{
    70  				hex: "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000e44616920537461626c65636f696e000000000000000000000000000000000000",
    71  			},
    72  			wantResult: "Dai Stablecoin",
    73  		},
    74  		{
    75  			name: "invalid string",
    76  			args: args{
    77  				// this is bytes32
    78  				hex: "0x506f6f6c65642045746865720000000000000000000000000000000000000000",
    79  			},
    80  			wantErr: true,
    81  		},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			gotResult, err := articulateEncodedString(tt.args.hex)
    86  			if (err != nil) != tt.wantErr {
    87  				t.Errorf("articulateEncodedString() error = %v, wantErr %v", err, tt.wantErr)
    88  				return
    89  			}
    90  			if gotResult != tt.wantResult {
    91  				t.Errorf("articulateEncodedString() = %v, want %v", gotResult, tt.wantResult)
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  func TestArticulateBytes32String(t *testing.T) {
    98  	type args struct {
    99  		hex string
   100  	}
   101  	tests := []struct {
   102  		name string
   103  		args args
   104  		want string
   105  	}{
   106  		{
   107  			name: "Pooled Ether",
   108  			args: args{
   109  				hex: "0x506f6f6c65642045746865720000000000000000000000000000000000000000",
   110  			},
   111  			want: "Pooled Ether",
   112  		},
   113  	}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			if got := articulateBytes32String(tt.args.hex); got != tt.want {
   117  				t.Errorf("articulateBytes32String() = %v, want %v", got, tt.want)
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  func TestArticulateStringOrBytes(t *testing.T) {
   124  	type args struct {
   125  		hex string
   126  	}
   127  	tests := []struct {
   128  		name    string
   129  		args    args
   130  		want    string
   131  		wantErr bool
   132  	}{
   133  		{
   134  			name: "bytes32",
   135  			args: args{
   136  				hex: "0x506f6f6c65642045746865720000000000000000000000000000000000000000",
   137  			},
   138  			want: "Pooled Ether",
   139  		},
   140  		{
   141  			name: "string",
   142  			args: args{
   143  				hex: "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000e44616920537461626c65636f696e000000000000000000000000000000000000",
   144  			},
   145  			want: "Dai Stablecoin",
   146  		},
   147  		{
   148  			name: "4 byte collision 0x8406d0897da43a33912995c6ffd792f1f2125cd4",
   149  			args: args{
   150  				hex: "0x0000000000000000000000000000000000000000000000000000000000000001",
   151  			},
   152  			want: "",
   153  		},
   154  	}
   155  	for _, tt := range tests {
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			got, err := ArticulateStringOrBytes(tt.args.hex)
   158  			if (err != nil) != tt.wantErr {
   159  				t.Errorf("ArticulateStringOrBytes() error = %v, wantErr %v", err, tt.wantErr)
   160  				return
   161  			}
   162  			if got != tt.want {
   163  				t.Errorf("ArticulateStringOrBytes() = %v, want %v", got, tt.want)
   164  			}
   165  		})
   166  	}
   167  }