github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/define_unmarshal_test.go (about)

     1  package lang_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	_ "github.com/lmorg/murex/builtins"
     8  	"github.com/lmorg/murex/lang"
     9  	"github.com/lmorg/murex/lang/types"
    10  	"github.com/lmorg/murex/test/count"
    11  )
    12  
    13  func TestUnmarshalArrayJsonString(t *testing.T) {
    14  	count.Tests(t, 1)
    15  
    16  	input := `["e","d","c","b","a"]` // lets prove the output retains sorting
    17  	output := `[e d c b a]`
    18  
    19  	lang.InitEnv()
    20  	fork := lang.ShellProcess.Fork(lang.F_CREATE_STDIN | lang.F_NO_STDOUT | lang.F_NO_STDERR)
    21  
    22  	_, err := fork.Stdin.Write([]byte(input))
    23  	if err != nil {
    24  		t.Error(err)
    25  		return
    26  	}
    27  
    28  	v, err := lang.UnmarshalData(fork.Process, types.Json)
    29  	if err != nil {
    30  		t.Error(err)
    31  		return
    32  	}
    33  
    34  	if fmt.Sprintf("%v", v) != output {
    35  		t.Error("Unmarshaller output doesn't match expected:")
    36  		t.Logf("  Input:    %s", input)
    37  		t.Logf("  Expected: '%s'", output)
    38  		t.Logf("  Actual:   '%v'", v)
    39  	}
    40  }
    41  
    42  func TestUnmarshalArrayJsonInt(t *testing.T) {
    43  	count.Tests(t, 1)
    44  
    45  	input := `[5,4,3,2,1]` // lets prove the output retains sorting
    46  	output := `[5 4 3 2 1]`
    47  
    48  	lang.InitEnv()
    49  	fork := lang.ShellProcess.Fork(lang.F_CREATE_STDIN | lang.F_NO_STDOUT | lang.F_NO_STDERR)
    50  
    51  	_, err := fork.Stdin.Write([]byte(input))
    52  	if err != nil {
    53  		t.Error(err)
    54  		return
    55  	}
    56  
    57  	v, err := lang.UnmarshalData(fork.Process, types.Json)
    58  	if err != nil {
    59  		t.Error(err)
    60  		return
    61  	}
    62  
    63  	if fmt.Sprintf("%v", v) != output {
    64  		t.Error("Unmarshaller output doesn't match expected:")
    65  		t.Logf("  Input:    %s", input)
    66  		t.Logf("  Expected: '%s'", output)
    67  		t.Logf("  Actual:   '%v'", v)
    68  	}
    69  }