github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/escape/escape_test.go (about)

     1  package escape
     2  
     3  import (
     4  	"testing"
     5  
     6  	_ "github.com/lmorg/murex/builtins/core/expressions"
     7  	_ "github.com/lmorg/murex/builtins/types/string"
     8  	"github.com/lmorg/murex/lang/types"
     9  	"github.com/lmorg/murex/test"
    10  )
    11  
    12  func TestEscape(t *testing.T) {
    13  	// as stdin
    14  
    15  	test.RunMethodTest(
    16  		t, cmdEscape, "escape",
    17  		`hello world`, types.String, nil,
    18  		`"hello world"`, nil)
    19  
    20  	test.RunMethodTest(
    21  		t, cmdEscape, "escape",
    22  		`"hello world"`, types.String, nil,
    23  		`"\"hello world\""`, nil)
    24  
    25  	// as a parameter
    26  
    27  	test.RunMethodTest(
    28  		t, cmdEscape, "escape",
    29  		``, types.String, []string{`hello`, `world`},
    30  		`"hello world"`, nil)
    31  
    32  	test.RunMethodTest(
    33  		t, cmdEscape, "escape",
    34  		``, types.String, []string{"hello world"},
    35  		`"hello world"`, nil)
    36  
    37  	test.RunMethodTest(
    38  		t, cmdEscape, "escape",
    39  		``, types.String, []string{`"hello world"`},
    40  		`"\"hello world\""`, nil)
    41  }
    42  
    43  func TestEscapeBang(t *testing.T) {
    44  	tests := []test.MurexTest{
    45  		{
    46  			Block:  `!escape "hello world"`,
    47  			Stdout: `hello world`,
    48  		},
    49  		{
    50  			Block:  `!escape ("hello world")`,
    51  			Stdout: `hello world`,
    52  		},
    53  		{
    54  			Block:  `!escape '\"hello world\"'`,
    55  			Stdout: `\"hello world\"`,
    56  		},
    57  		{
    58  			Block:  `!escape '\"hello\ world\"'`,
    59  			Stdout: `\"hello\ world\"`,
    60  		},
    61  		{
    62  			Block:  `!escape "foo &amp bar"`,
    63  			Stdout: `foo & bar`,
    64  		},
    65  	}
    66  
    67  	test.RunMurexTests(tests, t)
    68  }
    69  
    70  func TestHtml(t *testing.T) {
    71  	// as stdin
    72  
    73  	test.RunMethodTest(
    74  		t, cmdHtml, "eschtml",
    75  		`foo & bar`, types.String, nil,
    76  		`foo & bar`, nil)
    77  
    78  	test.RunMethodTest(
    79  		t, cmdHtml, "eschtml",
    80  		`"foo & bar"`, types.String, nil,
    81  		`"foo & bar"`, nil)
    82  
    83  	// as a parameter
    84  
    85  	test.RunMethodTest(
    86  		t, cmdHtml, "escape",
    87  		``, types.String, []string{`foo`, `&`, `bar`},
    88  		`foo & bar`, nil)
    89  
    90  	test.RunMethodTest(
    91  		t, cmdHtml, "escape",
    92  		``, types.String, []string{"foo & bar"},
    93  		`foo & bar`, nil)
    94  
    95  	test.RunMethodTest(
    96  		t, cmdHtml, "escape",
    97  		``, types.String, []string{`"foo & bar"`},
    98  		`"foo & bar"`, nil)
    99  }
   100  
   101  func TestHtmlBang(t *testing.T) {
   102  	tests := []test.MurexTest{
   103  		{
   104  			Block:  `!eschtml "foo &amp bar"`,
   105  			Stdout: `foo & bar`,
   106  		},
   107  	}
   108  
   109  	test.RunMurexTests(tests, t)
   110  }
   111  
   112  func TestUrl(t *testing.T) {
   113  	// as stdin
   114  
   115  	test.RunMethodTest(
   116  		t, cmdUrl, "escurl",
   117  		`foo bar`, types.String, nil,
   118  		`foo%20bar`, nil)
   119  
   120  	// as a parameter
   121  
   122  	test.RunMethodTest(
   123  		t, cmdUrl, "escurl",
   124  		``, types.String, []string{`foo`, `bar`},
   125  		`foo%20bar`, nil)
   126  
   127  	test.RunMethodTest(
   128  		t, cmdUrl, "escurl",
   129  		``, types.String, []string{"foo bar"},
   130  		`foo%20bar`, nil)
   131  
   132  }
   133  
   134  func TestUrlBang(t *testing.T) {
   135  	tests := []test.MurexTest{
   136  		{
   137  			Block:  `!escurl foo%20bar`,
   138  			Stdout: `foo bar`,
   139  		},
   140  	}
   141  
   142  	test.RunMurexTests(tests, t)
   143  }
   144  
   145  func TestCli(t *testing.T) {
   146  	// as stdin
   147  
   148  	test.RunMethodTest(
   149  		t, cmdEscapeCli, "esccli",
   150  		`foo bar`, types.String, nil,
   151  		"foo\\ bar\n", nil)
   152  }
   153  
   154  func TestCli2(t *testing.T) {
   155  	tests := []test.MurexTest{
   156  		{
   157  			Block:  `esccli foo bar`,
   158  			Stdout: "foo bar\n",
   159  		},
   160  		{
   161  			Block:  `esccli "foo bar"`,
   162  			Stdout: "foo\\ bar\n",
   163  		},
   164  	}
   165  
   166  	test.RunMurexTests(tests, t)
   167  }
   168  
   169  func TestCliBang(t *testing.T) {
   170  	tests := []test.MurexTest{
   171  		{
   172  			Block:   `!esccli foo\ bar`,
   173  			Stderr:  "executable file not found",
   174  			ExitNum: 1,
   175  		},
   176  	}
   177  
   178  	test.RunMurexTestsRx(tests, t)
   179  }