1// Copyright (c) 2021, Peter Ohler, All rights reserved. 2 3package asm_test 4 5import ( 6 "testing" 7 "time" 8 9 "github.com/ohler55/ojg/asm" 10 "github.com/ohler55/ojg/sen" 11 "github.com/ohler55/ojg/tt" 12) 13 14func TestSortShallow(t *testing.T) { 15 root := testPlan(t, 16 `[ 17 [set $.asm.a [sort [b a c] @]] 18 [set $.asm.b [sort [2 5 1] @]] 19 ]`, 20 "{src: []}", 21 ) 22 tt.Equal(t, `{a:[a b c] b:[1 2 5]}`, sen.String(root["asm"], &sopt)) 23} 24 25func TestSortDeep(t *testing.T) { 26 root := testPlan(t, 27 `[ 28 [set $.asm [sort [{x:2}{x:5}{x:1}] @.x]] 29 ]`, 30 "{src: []}", 31 ) 32 tt.Equal(t, `[{x:1}{x:2}{x:5}]`, sen.String(root["asm"], &sopt)) 33} 34 35func TestSortTime(t *testing.T) { 36 root := testPlan(t, 37 `[ 38 [set $.asm [sort [list [time "2021-02-09T00:00:00Z"][time "2021-01-05T00:00:00Z"]] @]] 39 ]`, 40 "{src: []}", 41 ) 42 tt.Equal(t, `["2021-01-05T00:00:00Z" "2021-02-09T00:00:00Z"]`, sen.String(root["asm"], &sopt)) 43} 44 45func TestSortArgCount(t *testing.T) { 46 p := asm.NewPlan([]interface{}{ 47 []interface{}{"sort", []interface{}{}, "@", 1}, 48 }) 49 err := p.Execute(map[string]interface{}{}) 50 tt.NotNil(t, err) 51} 52 53func TestSortArgType(t *testing.T) { 54 p := asm.NewPlan([]interface{}{ 55 []interface{}{"sort", 1, "@"}, 56 }) 57 err := p.Execute(map[string]interface{}{}) 58 tt.NotNil(t, err) 59} 60 61func TestSortArgType2(t *testing.T) { 62 p := asm.NewPlan([]interface{}{ 63 []interface{}{"sort", []interface{}{}, 1}, 64 }) 65 err := p.Execute(map[string]interface{}{}) 66 tt.NotNil(t, err) 67} 68 69func TestSortMixedString(t *testing.T) { 70 p := asm.NewPlan([]interface{}{ 71 []interface{}{"sort", []interface{}{"x", 1}, "@"}, 72 }) 73 err := p.Execute(map[string]interface{}{}) 74 tt.NotNil(t, err) 75} 76 77func TestSortMixedNum(t *testing.T) { 78 p := asm.NewPlan([]interface{}{ 79 []interface{}{"sort", []interface{}{1, "x"}, "@"}, 80 }) 81 err := p.Execute(map[string]interface{}{}) 82 tt.NotNil(t, err) 83} 84 85func TestSortMixedTime(t *testing.T) { 86 p := asm.NewPlan([]interface{}{ 87 []interface{}{"sort", []interface{}{1, time.Now(), time.Now().Add(time.Hour)}, "@"}, 88 }) 89 err := p.Execute(map[string]interface{}{}) 90 tt.NotNil(t, err) 91} 92 93func TestSortWrongType(t *testing.T) { 94 p := asm.NewPlan([]interface{}{ 95 []interface{}{"sort", []interface{}{true, false}, "@"}, 96 }) 97 err := p.Execute(map[string]interface{}{}) 98 tt.NotNil(t, err) 99} 100