@@ -576,85 +576,86 @@ default:
576576 match = re.Match(body)` + "\n " )
577577 b .WriteString (` if !match` + " {\n " )
578578 return b .String ()
579- } else {
579+ }
580580
581- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
582- // 1b. Is the expected value a literal value
583- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
584- // TODO: Match on literal value
585- // output = fmt.Sprintf("if true { // TODO, MatchBodyLiteral: %s => %v\n", a.operation, a.payload)
586- output = `if strings.HasPrefix(string(body), "[") {
581+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
582+ // 1b. Is the expected value a literal value?
583+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
584+ // TODO: Match on literal value
585+ // output = fmt.Sprintf("if true { // TODO, MatchBodyLiteral: %s => %v\n", a.operation, a.payload)
586+ output = `if strings.HasPrefix(string(body), "[") {
587587 match = !reflect.DeepEqual(fmt.Sprintf("%#v", slic), ` + fmt .Sprintf ("%#v" , val ) + `)
588588 } else {
589589 match = !reflect.DeepEqual(fmt.Sprintf("%#v", mapi), ` + fmt .Sprintf ("%#v" , val ) + `)
590590 }
591591 if !match {` + "\n "
592- return output
593- }
592+ return output
594593
595594 // panic(fmt.Sprintf("MatchUnknown: %q => %v\n", a.operation, a.payload))
595+ }
596596
597- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
598- // 2. Match on JSON/YAML field or numbered item
599- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
600- } else {
601- subject = expand (key )
602- expected = strings .TrimSpace (fmt .Sprintf ("%v" , val ))
603-
604- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
605- // 2a. Is the expected value a regex pattern?
606- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
607- if strings .HasPrefix (expected , "/" ) {
608- var pattern string
609- pattern = expected
610- pattern = strings .Replace (pattern , "/" , "" , - 1 )
611- pattern = strings .Replace (pattern , "\n " , "" , - 1 )
612- pattern = strings .Replace (pattern , " " , `\s*` , - 1 )
597+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
598+ // 2. Match on JSON/YAML field or numbered item
599+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
613600
614- b .WriteString (`re, err = regexp.Compile("(?m)" + ` + "`" + pattern + "`" + `)
601+ subject = expand (key )
602+ expected = strings .TrimSpace (fmt .Sprintf ("%v" , val ))
603+
604+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
605+ // 2a. Is the expected value a regex pattern?
606+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
607+ if strings .HasPrefix (expected , "/" ) {
608+ var pattern string
609+ pattern = expected
610+ pattern = strings .Replace (pattern , "/" , "" , - 1 )
611+ pattern = strings .Replace (pattern , "\n " , "" , - 1 )
612+ pattern = strings .Replace (pattern , " " , `\s*` , - 1 )
613+
614+ b .WriteString (`re, err = regexp.Compile("(?m)" + ` + "`" + pattern + "`" + `)
615615 if err != nil {
616616 t.Fatalf("Regex compile error: %s", err)
617617 }
618618
619619 match = re.MatchString(fmt.Sprintf("%v", ` + escape (subject ) + `))` + "\n " )
620- b .WriteString (` if !match` + " {\n " )
621- return b .String ()
620+ b .WriteString (` if !match` + " {\n " )
621+ return b .String ()
622+ }
622623
623- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
624- // 2b. Is the value a regular value?
625- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
626- } else {
627- // fmt.Printf("subject: %s, val: %T, %v \n", subject, val, val)
628-
629- nilGuard := catchnil (subject )
630- switch val .(type ) {
631-
632- // --------------------------------------------------------------------------------
633- case nil :
634- output += ` if ` + escape (subject ) + ` != nil` + " {\n "
635-
636- // --------------------------------------------------------------------------------
637- case bool :
638- output += ` if ` +
639- nilGuard +
640- " || \n " +
641- `fmt.Sprintf("%v", ` + escape (subject ) + `) != ` +
642- `fmt.Sprintf("%v", ` + expected + `)` +
643- " {\n "
644- case int , float64 :
645- // Date range special case
646- // From & to are set in yaml tests as integer
647- // but ES returns a scientific notation float
648- var specialCase string
649- if strings .HasSuffix (key , "from" ) || strings .HasSuffix (key , "to" ) {
650- specialCase = `else if floatValue, err := actual.(encjson.Number).Float64(); err == nil {
624+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
625+ // 2b. Is the value a regular value?
626+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
627+
628+ // fmt.Printf("subject: %s, val: %T, %v \n", subject, val, val)
629+
630+ nilGuard := catchnil (subject )
631+ switch val .(type ) {
632+
633+ // --------------------------------------------------------------------------------
634+ case nil :
635+ output += ` if ` + escape (subject ) + ` != nil` + " {\n "
636+
637+ // --------------------------------------------------------------------------------
638+ case bool :
639+ output += ` if ` +
640+ nilGuard +
641+ " || \n " +
642+ `fmt.Sprintf("%v", ` + escape (subject ) + `) != ` +
643+ `fmt.Sprintf("%v", ` + expected + `)` +
644+ " {\n "
645+ case int , float64 :
646+ // Date range special case
647+ // From & to are set in yaml tests as integer
648+ // but ES returns a scientific notation float
649+ var specialCase string
650+ if strings .HasSuffix (key , "from" ) || strings .HasSuffix (key , "to" ) {
651+ specialCase = `else if floatValue, err := actual.(encjson.Number).Float64(); err == nil {
651652 actual = strconv.FormatFloat(floatValue, 'f', -1, 64)
652653 } `
653- }
654+ }
654655
655- r := strings .NewReplacer (`"` , "" , `\` , "" )
656- rkey := r .Replace (key )
657- output += ` if ` + nilGuard + ` { t.Error("Expected [` + rkey + `] to not be nil") }
656+ r := strings .NewReplacer (`"` , "" , `\` , "" )
657+ rkey := r .Replace (key )
658+ output += ` if ` + nilGuard + ` { t.Error("Expected [` + rkey + `] to not be nil") }
658659 actual = ` + escape (subject ) + `
659660 if intValue, ok := actual.(int); ok {
660661 actual = fmt.Sprint(intValue)
@@ -675,53 +676,51 @@ default:
675676 if !assertion {
676677 t.Logf("%v != %v", actual, expected)` + "\n "
677678
678- // --------------------------------------------------------------------------------
679- case string :
680- output += ` if ` +
681- nilGuard +
682- " || \n " +
683- `strings.TrimSpace(fmt.Sprintf("%s", ` + escape (subject ) + `)) != `
684- if strings .HasPrefix (expected , "$" ) {
685- // Remove brackets if we compare to a stashed value replaced in the body.
686- expected = strings .NewReplacer ("{" , "" , "}" , "" ).Replace (expected )
687- output += `strings.TrimSpace(fmt.Sprintf("%s", ` + `stash["` + expected + `"]` + `))`
688- } else {
689- output += `strings.TrimSpace(fmt.Sprintf("%s", ` + strconv .Quote (expected ) + `))`
690- }
691- output += " {\n "
692-
693- // --------------------------------------------------------------------------------
694- case map [interface {}]interface {}, map [string ]interface {}:
695- // We cannot reliably serialize to json and compare the json outputs: YAML responses are parsed as
696- // a map[interface{}]interface{} that encoding/json fails to marshall
697- // See https://play.golang.org/p/jhcXwg5dIrn
698- expectedOutput := flattenPayload (val )
699- expectedPayload := fmt .Sprintf ("%#v" , expectedOutput )
700-
701- expectedPayload = strings .ReplaceAll (expectedPayload , "map[interface {}]interface {}" , "map[string]interface {}" )
702- output = ` actual = fmt.Sprintf("%v",` + escape (subject ) + `)
679+ // --------------------------------------------------------------------------------
680+ case string :
681+ output += ` if ` +
682+ nilGuard +
683+ " || \n " +
684+ `strings.TrimSpace(fmt.Sprintf("%s", ` + escape (subject ) + `)) != `
685+ if strings .HasPrefix (expected , "$" ) {
686+ // Remove brackets if we compare to a stashed value replaced in the body.
687+ expected = strings .NewReplacer ("{" , "" , "}" , "" ).Replace (expected )
688+ output += `strings.TrimSpace(fmt.Sprintf("%s", ` + `stash["` + expected + `"]` + `))`
689+ } else {
690+ output += `strings.TrimSpace(fmt.Sprintf("%s", ` + strconv .Quote (expected ) + `))`
691+ }
692+ output += " {\n "
693+
694+ // --------------------------------------------------------------------------------
695+ case map [interface {}]interface {}, map [string ]interface {}:
696+ // We cannot reliably serialize to json and compare the json outputs: YAML responses are parsed as
697+ // a map[interface{}]interface{} that encoding/json fails to marshall
698+ // See https://play.golang.org/p/jhcXwg5dIrn
699+ expectedOutput := flattenPayload (val )
700+ expectedPayload := fmt .Sprintf ("%#v" , expectedOutput )
701+
702+ expectedPayload = strings .ReplaceAll (expectedPayload , "map[interface {}]interface {}" , "map[string]interface {}" )
703+ output = ` actual = fmt.Sprintf("%v",` + escape (subject ) + `)
703704 expected = fmt.Sprintf("%v",` + expectedPayload + `)
704705 if actual != expected {` + "\n "
705706
706- // --------------------------------------------------------------------------------
707- case []interface {}:
708- expectedPayload := fmt .Sprintf ("%#v" , val )
709- expectedPayload = strings .ReplaceAll (expectedPayload , "map[interface {}]interface {}" , "map[string]interface {}" )
710- output = ` actual, _ = encjson.Marshal(` + escape (subject ) + `)
707+ // --------------------------------------------------------------------------------
708+ case []interface {}:
709+ expectedPayload := fmt .Sprintf ("%#v" , val )
710+ expectedPayload = strings .ReplaceAll (expectedPayload , "map[interface {}]interface {}" , "map[string]interface {}" )
711+ output = ` actual, _ = encjson.Marshal(` + escape (subject ) + `)
711712 expected, _ = encjson.Marshal(` + expectedPayload + `)
712713 if fmt.Sprintf("%s", actual) != fmt.Sprintf("%s", expected) {` + "\n "
713714
714- // --------------------------------------------------------------------------------
715- default :
716- output += ` if true { // TODO, MatchMissingType: ` + fmt .Sprintf ("<%[1]T>%[1]v" , val ) + "\n "
717- output += ` // Subject: ` + subject + "\n "
718- output += ` // Value: ` + fmt .Sprintf ("%#v" , val ) + "\n "
719- }
720-
721- return output
722- }
715+ // --------------------------------------------------------------------------------
716+ default :
717+ output += ` if true { // TODO, MatchMissingType: ` + fmt .Sprintf ("<%[1]T>%[1]v" , val ) + "\n "
718+ output += ` // Subject: ` + subject + "\n "
719+ output += ` // Value: ` + fmt .Sprintf ("%#v" , val ) + "\n "
723720 }
724721
722+ return output
723+
725724 // ================================================================================
726725 default :
727726 return fmt .Sprintf ("if true { // TODO, Unimplemented: %q => %v\n " , a .operation , a .payload )
@@ -768,9 +767,8 @@ func (a Assertion) Error() string {
768767
769768 if strings .HasPrefix (expected , "/" ) {
770769 return `t.Errorf("Expected [$body] to match pattern: %s", re)`
771- } else {
772- output = `Expected [$body] to match value: ` + escape (expected )
773770 }
771+ output = `Expected [$body] to match value: ` + escape (expected )
774772
775773 } else { // 2. Match on JSON/YAML field or numbered item
776774 if key == "" {
@@ -782,9 +780,10 @@ func (a Assertion) Error() string {
782780 expected = strings .Replace (expected , `/` , `|` , - 1 )
783781
784782 return `t.Errorf("Expected [` + strings .Trim (strconv .Quote (escape (key )), `"` ) + `] to match pattern: %s", re)`
785- } else { // 2b. Is the value to match a regular value?
786- output = `Expected [` + strings .Trim (strconv .Quote (escape (key )), `"` ) + `] to match '` + escape (expected ) + `'`
787783 }
784+
785+ // 2b. Is the value to match a regular value?
786+ output = `Expected [` + strings .Trim (strconv .Quote (escape (key )), `"` ) + `] to match '` + escape (expected ) + `'`
788787 }
789788 }
790789
0 commit comments