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

     1  package mkarray
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/test/count"
     7  )
     8  
     9  func TestGetCase(t *testing.T) {
    10  	count.Tests(t, 4)
    11  
    12  	if getCase("foobar") != caseLower {
    13  		t.Error("`foobar` not being detected as lower case")
    14  	}
    15  
    16  	if getCase("Foobar") != caseFirst {
    17  		t.Error("`Foobar` not being detected as first case")
    18  	}
    19  
    20  	if getCase("FOOBAR") != caseUpper {
    21  		t.Error("`FOOBAR` not being detected as upper case")
    22  	}
    23  
    24  	if getCase("Foo Bar") != caseTitle {
    25  		t.Error("`Foo Bar` not being detected as title case")
    26  	}
    27  }
    28  
    29  func TestGetCaseDate(t *testing.T) {
    30  	count.Tests(t, 3)
    31  
    32  	if getCase("1-jan-20") != caseLDate {
    33  		t.Error("`1-jan-20` not being detected as lower case")
    34  	}
    35  
    36  	if getCase("1-Jan-20") != caseTitle {
    37  		t.Error("`Foobar` not being detected as first case")
    38  	}
    39  
    40  	if getCase("1-JAN-20") != caseUpper {
    41  		t.Error("`FOOBAR` not being detected as upper case")
    42  	}
    43  }
    44  
    45  func TestSetCase(t *testing.T) {
    46  	count.Tests(t, 3)
    47  
    48  	input := "foo bar"
    49  
    50  	expected := "foo bar"
    51  	output := setCase(input, caseLower)
    52  	if output != expected {
    53  		t.Error("setCase not lower casing correctly")
    54  		t.Log("  input:    ", input)
    55  		t.Log("  output:   ", output)
    56  		t.Log("  expected: ", expected)
    57  	}
    58  
    59  	expected = "Foo bar"
    60  	output = setCase(input, caseFirst)
    61  	if output != expected {
    62  		t.Error("setCase not title casing correctly")
    63  		t.Log("  input:    ", input)
    64  		t.Log("  output:   ", output)
    65  		t.Log("  expected: ", expected)
    66  	}
    67  
    68  	expected = "Foo Bar"
    69  	output = setCase(input, caseTitle)
    70  	if output != expected {
    71  		t.Error("setCase not title casing correctly")
    72  		t.Log("  input:    ", input)
    73  		t.Log("  output:   ", output)
    74  		t.Log("  expected: ", expected)
    75  	}
    76  
    77  	expected = "FOO BAR"
    78  	output = setCase(input, caseUpper)
    79  	if output != expected {
    80  		t.Error("setCase not upper casing correctly")
    81  		t.Log("  input:    ", input)
    82  		t.Log("  output:   ", output)
    83  		t.Log("  expected: ", expected)
    84  	}
    85  }
    86  
    87  func TestSetCaseDate(t *testing.T) {
    88  	count.Tests(t, 4)
    89  
    90  	input := "1-jAN-20"
    91  
    92  	expected := "1-jAN-20"
    93  	output := setCase(input, caseLower)
    94  	if output != expected {
    95  		t.Error("setCase not lower casing correctly")
    96  		t.Log("  input:    ", input)
    97  		t.Log("  output:   ", output)
    98  		t.Log("  expected: ", expected)
    99  	}
   100  
   101  	expected = "1-jan-20"
   102  	output = setCase(input, caseLDate)
   103  	if output != expected {
   104  		t.Error("setCase not lower casing correctly")
   105  		t.Log("  input:    ", input)
   106  		t.Log("  output:   ", output)
   107  		t.Log("  expected: ", expected)
   108  	}
   109  
   110  	expected = "1-JAN-20"
   111  	output = setCase(input, caseTitle)
   112  	if output != expected {
   113  		t.Error("setCase not title casing correctly")
   114  		t.Log("  input:    ", input)
   115  		t.Log("  output:   ", output)
   116  		t.Log("  expected: ", expected)
   117  	}
   118  
   119  	expected = "1-JAN-20"
   120  	output = setCase(input, caseUpper)
   121  	if output != expected {
   122  		t.Error("setCase not upper casing correctly")
   123  		t.Log("  input:    ", input)
   124  		t.Log("  output:   ", output)
   125  		t.Log("  expected: ", expected)
   126  	}
   127  
   128  	expected = "1-jAN-20"
   129  	output = setCase(input, caseTDate)
   130  	if output != expected {
   131  		t.Error("setCase not upper casing correctly")
   132  		t.Log("  input:    ", input)
   133  		t.Log("  output:   ", output)
   134  		t.Log("  expected: ", expected)
   135  	}
   136  }
   137  
   138  // TestOptimisedSetCase checks that nobody tries to "bug fix" the setCase()
   139  // function with lowercasing already lowercased elements
   140  func TestOptimisedSetCase(t *testing.T) {
   141  	count.Tests(t, 2)
   142  
   143  	input := "fooBar"
   144  
   145  	expected := "foobar"
   146  	output := setCase(input, caseLower)
   147  	if output == expected {
   148  		t.Error("setCase(s, caseLower) has been changed to lower case when shouldn't")
   149  		t.Log("  input:    ", input)
   150  		t.Log("  output:   ", output)
   151  		t.Log("  expected: ", input)
   152  		t.Log("All elements should be lowercase by default, so we don't need to waste time lowercasing the string")
   153  	}
   154  
   155  	expected = "Foobar"
   156  	output = setCase(input, caseTitle)
   157  	if output == expected {
   158  		t.Error("setCase(s, caseTitle) has been changed to lower case when shouldn't")
   159  		t.Log("  input:    ", input)
   160  		t.Log("  output:   ", output)
   161  		t.Log("  expected: ", "FooBar")
   162  		t.Log("All elements should be lowercase by default, so we don't need to waste time lowercasing most of the string")
   163  	}
   164  }