...

Source file src/cuelang.org/go/cue/format_test.go

Documentation: cuelang.org/go/cue

     1  // Copyright 2021 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cue_test
    16  
    17  import (
    18  	"fmt"
    19  	"path"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue"
    23  	"cuelang.org/go/cue/cuecontext"
    24  )
    25  
    26  func ExampleValue_Format() {
    27  	ctx := cuecontext.New()
    28  
    29  	v := ctx.CompileString(`
    30  		a: 2 + b
    31  		b: *3 | int
    32  		s: "foo\nbar"
    33  	`)
    34  
    35  	fmt.Println("### ALL")
    36  	fmt.Println(v)
    37  	fmt.Println("---")
    38  	fmt.Printf("%#v\n", v)
    39  	fmt.Println("---")
    40  	fmt.Printf("%+v\n", v)
    41  
    42  	a := v.LookupPath(cue.ParsePath("a"))
    43  	fmt.Println("\n### INT")
    44  	fmt.Printf("%%v:   %v\n", a)
    45  	fmt.Printf("%%05d: %05d\n", a)
    46  
    47  	s := v.LookupPath(cue.ParsePath("s"))
    48  	fmt.Println("\n### STRING")
    49  	fmt.Printf("%%v: %v\n", s)
    50  	fmt.Printf("%%s: %s\n", s)
    51  	fmt.Printf("%%q: %q\n", s)
    52  
    53  	v = ctx.CompileString(`
    54  		#Def: a: [string]: int
    55  		b: #Def
    56  		b: a: {
    57  			a: 3
    58  			b: 3
    59  		}
    60  	`)
    61  	b := v.LookupPath(cue.ParsePath("b.a"))
    62  	fmt.Println("\n### DEF")
    63  	fmt.Println(b)
    64  	fmt.Println("---")
    65  	// This will indicate that the result is closed by including a hidden
    66  	// definition.
    67  	fmt.Printf("%#v\n", b)
    68  
    69  	// Output:
    70  	// ### ALL
    71  	// {
    72  	// 	a: 5
    73  	// 	b: *3 | int
    74  	// 	s: """
    75  	// 		foo
    76  	// 		bar
    77  	// 		"""
    78  	// }
    79  	// ---
    80  	// a: 2 + b
    81  	// b: *3 | int
    82  	// s: "foo\nbar"
    83  	// ---
    84  	// {
    85  	// 	a: 5
    86  	// 	b: 3
    87  	// 	s: """
    88  	// 		foo
    89  	// 		bar
    90  	// 		"""
    91  	// }
    92  	//
    93  	// ### INT
    94  	// %v:   5
    95  	// %05d: 00005
    96  	//
    97  	// ### STRING
    98  	// %v: """
    99  	// 	foo
   100  	// 	bar
   101  	// 	"""
   102  	// %s: foo
   103  	// bar
   104  	// %q: "foo\nbar"
   105  	//
   106  	// ### DEF
   107  	// {
   108  	// 	a: 3
   109  	// 	b: 3
   110  	// }
   111  	// ---
   112  	// _#def
   113  	// _#def: {
   114  	// 	{
   115  	// 		[string]: int
   116  	// 	}
   117  	// 	a: 3
   118  	// 	b: 3
   119  	// }
   120  }
   121  
   122  func TestFormat(t *testing.T) {
   123  	tests := func(s ...string) (a [][2]string) {
   124  		for i := 0; i < len(s); i += 2 {
   125  			a = append(a, [2]string{s[i], s[i+1]})
   126  		}
   127  		return a
   128  	}
   129  	testCases := []struct {
   130  		desc string
   131  		in   string
   132  		out  [][2]string
   133  	}{{
   134  		desc: "int",
   135  		in:   `12 + 14`,
   136  		out: tests(
   137  			"%#v", "26",
   138  			"%d", "26",
   139  			"%o", "32",
   140  			"%O", "0o32",
   141  			"%x", "1a",
   142  			"%X", "1A",
   143  			"%q", `"26"`,
   144  			"%0.3d", "026",
   145  		),
   146  	}, {
   147  		desc: "float",
   148  		in:   `12.2 + 14.4`,
   149  		out: tests(
   150  			"%#v", "26.6",
   151  			"%5f", " 26.6",
   152  			"%e", "2.66e+1",
   153  			"%08E", "02.66E+1",
   154  			"%g", "26.6",
   155  			"%3G", "26.6",
   156  		),
   157  	}, {
   158  		desc: "strings",
   159  		in:   `"string"`,
   160  		out: tests(
   161  			"%v", `"string"`,
   162  			"%s", "string",
   163  			"%x", "737472696e67",
   164  			"%X", "737472696E67",
   165  		),
   166  	}, {
   167  		desc: "multiline string",
   168  		in: `"""
   169  		foo
   170  		bar
   171  		"""`,
   172  		out: tests(
   173  			"%#v", `"""
   174  	foo
   175  	bar
   176  	"""`,
   177  			"%s", "foo\nbar",
   178  			"%q", `"foo\nbar"`,
   179  		),
   180  	}, {
   181  		desc: "multiline bytes",
   182  		in: `'''
   183  			foo
   184  			bar
   185  			'''`,
   186  		out: tests(
   187  			"%#v", `'''
   188  	foo
   189  	bar
   190  	'''`,
   191  			"%s", "foo\nbar",
   192  			"%q", `"foo\nbar"`,
   193  		),
   194  	}, {
   195  		desc: "interpolation",
   196  		in: `
   197  		#D: {
   198  			a: string
   199  			b: "hello \(a)"
   200  		}
   201  		d: #D
   202  		d: a: "world"
   203  		x: *1 | int
   204  		`,
   205  		out: tests(
   206  			"%v", `{
   207  	d: {
   208  		a: "world"
   209  		b: "hello world"
   210  	}
   211  	x: *1 | int
   212  }`,
   213  			"%#v", `#D: {
   214  	a: string
   215  	b: "hello \(a)"
   216  }
   217  d: #D & {
   218  	a: "world"
   219  }
   220  x: *1 | int`,
   221  			"%+v", `{
   222  	d: {
   223  		a: "world"
   224  		b: "hello world"
   225  	}
   226  	x: 1
   227  }`,
   228  		),
   229  	}, {
   230  		desc: "indent",
   231  		in: `
   232  a: {
   233  	b: """
   234  		foo
   235  		bar
   236  		"""
   237  	c: int
   238  }`,
   239  		out: tests(
   240  			"%v", `{
   241  	a: {
   242  		b: """
   243  			foo
   244  			bar
   245  			"""
   246  		c: int
   247  	}
   248  }`,
   249  			"%3v", `{
   250  				a: {
   251  					b: """
   252  						foo
   253  						bar
   254  						"""
   255  					c: int
   256  				}
   257  			}`,
   258  			"%.1v", `{
   259   a: {
   260    b: """
   261     foo
   262     bar
   263     """
   264    c: int
   265   }
   266  }`,
   267  			"%3.1v", `{
   268      a: {
   269       b: """
   270        foo
   271        bar
   272        """
   273       c: int
   274      }
   275     }`,
   276  		),
   277  	}, {
   278  		desc: "imports",
   279  		in: `
   280  		import "strings"
   281  		a: strings.Contains("foo")
   282  		`,
   283  		out: tests(
   284  			"%v", `{
   285  	a: strings.Contains("foo")
   286  }`,
   287  			"%+v", `{
   288  	a: strings.Contains("foo")
   289  }`,
   290  			"%#v", `import "strings"
   291  
   292  a: strings.Contains("foo")`,
   293  		),
   294  	}}
   295  	ctx := cuecontext.New()
   296  	for _, tc := range testCases {
   297  		for _, test := range tc.out {
   298  			t.Run(path.Join(tc.desc, test[0]), func(t *testing.T) {
   299  				v := ctx.CompileString(tc.in)
   300  				got := fmt.Sprintf(test[0], v)
   301  				if got != test[1] {
   302  					t.Errorf(" got: %s\nwant: %s", got, test[1])
   303  				}
   304  			})
   305  		}
   306  	}
   307  }
   308  

View as plain text