// Copyright 2019 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !gen

package gocode

import (
	"bytes"
	"regexp"
	"strings"
	"testing"

	"cuelang.org/go/cue/errors"
	"cuelang.org/go/encoding/gocode/testdata/pkg1"
	"cuelang.org/go/encoding/gocode/testdata/pkg2"
	"cuelang.org/go/internal/cuetdtest"
	"cuelang.org/go/internal/cuetest"
)

type validator interface {
	Validate() error
}

func TestPackages(t *testing.T) {
	type testCase struct {
		name  string
		value validator
		want  string `test:"update"`
	}

	testCases := []testCase{{
		name:  "failing int",
		value: pkg2.PickMe(4),
		want:  "invalid value 4 (out of bound >5):\n    pkg2/instance.cue:x:x",
	}, {
		name:  "failing field with validator",
		value: &pkg1.OtherStruct{A: "car"},
		want: `2 errors in empty disjunction:
conflicting values {A:"car",P:0} and null (mismatched types struct and null)
A: invalid value "car" (does not satisfy strings.ContainsAny("X")):
    pkg1/instance.cue:x:x
    pkg1/instance.cue:x:x`,
	}, {
		name:  "failing field of type int",
		value: &pkg1.MyStruct{A: 11, B: "dog"},
		want: `2 errors in empty disjunction:
conflicting values {A:11,B:"dog",T:"0001-01-01T00:00:00Z"} and null (mismatched types struct and null)
A: invalid value 11 (out of bound <=10):
    pkg1/instance.cue:x:x`,
	}, {
		name:  "failing nested struct ",
		value: &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "car", P: 6}},
		want: `4 errors in empty disjunction:
conflicting values {A:5,B:"dog",T:"0001-01-01T00:00:00Z",O:{A:"car",P:6}} and null (mismatched types struct and null)
O: 2 errors in empty disjunction:
O: conflicting values {A:"car",P:6} and null (mismatched types struct and null):
    pkg1/instance.cue:x:x
O.A: invalid value "car" (does not satisfy strings.ContainsAny("X")):
    pkg1/instance.cue:x:x
    pkg1/instance.cue:x:x`,
	}, {
		name:  "fail nested struct of different package",
		value: &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "X", P: 4}},
		want: `4 errors in empty disjunction:
conflicting values {A:5,B:"dog",T:"0001-01-01T00:00:00Z",O:{A:"X",P:4}} and null (mismatched types struct and null)
O: 2 errors in empty disjunction:
O: conflicting values {A:"X",P:4} and null (mismatched types struct and null):
    pkg1/instance.cue:x:x
O.P: invalid value 4 (out of bound >5):
    pkg2/instance.cue:x:x`,
	}, {
		name: "fail nested struct of transitively imported package",
		value: &pkg1.MyStruct{
			A: 5,
			B: "dog",
			I: &pkg2.ImportMe{A: 1000, B: "z"},
		},
		want: `4 errors in empty disjunction:
conflicting values {A:5,B:"dog",T:"0001-01-01T00:00:00Z",I:{a:1000,b:"z"}} and null (mismatched types struct and null)
I: 2 errors in empty disjunction:
I: conflicting values {a:1000,b:"z"} and null (mismatched types struct and null):
    pkg1/instance.cue:x:x
I.a: invalid value 1000 (out of bound <100):
    pkg2/instance.cue:x:x`}, {
		name: "all good",
		value: &pkg1.MyStruct{
			A: 5,
			B: "dog",
			I: &pkg2.ImportMe{A: 50, B: "a"},
		},
		want: `nil`,
	}}

	cuetdtest.Run(t, testCases, func(t *cuetdtest.T, tc *testCase) {
		t.Update(cuetest.UpdateGoldenFiles)

		got := errStr(tc.value.Validate())
		// The expected values in the test table have extra whitespace, but we trim both
		// sides for comparison. We need to store the trimmed version back for auto-update.
		got = strings.TrimSpace(got)
		tc.want = strings.TrimSpace(tc.want)
		t.Equal(got, tc.want)
	})
}

func errStr(err error) string {
	if err == nil {
		return "nil"
	}
	buf := &bytes.Buffer{}
	errors.Print(buf, err, nil)
	r := regexp.MustCompile(`.cue:\d+:\d+`)
	return r.ReplaceAllString(buf.String(), ".cue:x:x")
}
