Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
doc/tutorial/basics: add test for tutorial
Browse files Browse the repository at this point in the history
test whether examples in tutorial produce the desired
results.

Closes #50.

Change-Id: I3c5b8a465e6ed98dad2962efd88ce246173250a6
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2178
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed Jun 7, 2019
1 parent 9cd516c commit 00c373d
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 52 deletions.
6 changes: 3 additions & 3 deletions doc/tutorial/basics/bottom.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ val: list[3]
<!-- result -->
`$ cue eval -i bottom.cue`
```
a: _|_
l: _|_
a: _|_ /* conflicting values: 4 != 5 */
l: [1, _|_ /* conflicting values: 2 != 3 */]
list: [0, 1, 2]
val: _|_
val: _|_ /* index 3 out of bounds */
```
3 changes: 1 addition & 2 deletions doc/tutorial/basics/coalesce.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ v: *n[0]&string | "default"
<!-- result -->
`$ cue eval coalesce.cue`
```
list: [ "Cat", "Mouse", "Dog" ]
list: ["Cat", "Mouse", "Dog"]
a: "Cat"
b: "None"
n: [null]
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial/basics/conditional.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ as `justification` is required yet no concrete value is given.
<!-- CUE editor -->
_conditional.cue:_
```
price: float
price: number
// Require a justification if price is too high
justification: string if price > 100
Expand Down
5 changes: 2 additions & 3 deletions doc/tutorial/basics/cycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ b: a - 100
```
x: 200
y: 100
a: _|_ // cycle detected
b: _|_ // cycle detected
a: _|_ /* cycle detected */
b: _|_ /* cycle detected */
```
10 changes: 8 additions & 2 deletions doc/tutorial/basics/cycleref.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ selectors: {name: "bar"}
<!-- result -->
`$ cue eval cycleref.cue`
```
labels: {app: "foo", name: "bar"}
selectors: {app: "foo", name: "bar"}
labels: {
name: "bar"
app: "foo"
}
selectors: {
name: "bar"
app: "foo"
}
```
2 changes: 1 addition & 1 deletion doc/tutorial/basics/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ protocol: *"udp" | "tcp"
`$ cue eval defaults.cue`
```
replicas: 1
protocol: *"tcp" | *"udp"
protocol: "tcp" | "udp" | *_|_
```
6 changes: 3 additions & 3 deletions doc/tutorial/basics/fieldcomp.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ a: [ "Barcelona", "Shanghai", "Munich" ]
`$ cue eval fieldcomp.cue`
```
barcelona: {
pos: 1
name: "Barcelona"
pos: 1
nameLen: 9
}
shanghai: {
pos: 2
name: "Shanghai"
pos: 2
nameLen: 8
}
munich: {
pos: 3
name: "Munich"
pos: 3
nameLen: 6
}
```
6 changes: 3 additions & 3 deletions doc/tutorial/basics/interpolfield.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ String interpolations may also be used in field names.
One cannot refer to generated fields with references.

<!-- CUE editor -->
_- genfield.cue:_
_genfield.cue:_
```
sandwich: {
type: "Cheese"
Expand All @@ -24,8 +24,8 @@ sandwich: {
```
sandwich: {
type: "Cheese"
hasCheese: true
hasButter: true
butterAndCheese: _|_ // unknown reference 'hasCheese'
butterAndCheese: _|_ /* reference "hasCheese" not found */
hasCheese: true
}
```
12 changes: 4 additions & 8 deletions doc/tutorial/basics/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ yourIP: [11, 1, 2, 3]
<!-- result -->
`$ cue eval -i lists.cue`
```
IP: [>=0 & <=255, >=0 & <=255, >=0 & <=255, >=0 & <=255]
PrivateIP:
[10, >=0 & <=255, >=0 & <=255, >=0 & <=255] |
[192, 168, >=0 & <=255, >=0 & <=255] |
[172, >=16 & <=32, >=0 & <=255, >=0 & <=255]
myIP: [10, 2, 3, 4]
yourIP: _|_
IP: [uint8, uint8, uint8, uint8]
PrivateIP: [10, uint8, uint8, uint8] | [192, 168, uint8, uint8] | [172, >=16 & <=32, uint8, uint8]
myIP: [10, 2, 3, 4]
yourIP: _|_ /* empty disjunction: [((10 & (int & >=0 & int & <=255)) & 11),((int & >=0 & int & <=255) & 1),((int & >=0 & int & <=255) & 2),((int & >=0 & int & <=255) & 3)] */
```
6 changes: 3 additions & 3 deletions doc/tutorial/basics/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _numbers.cue:_
a: int
a: 4 // type int
b: float
b: number
b: 4 // type float
c: int
Expand All @@ -33,7 +33,7 @@ d: 4 // will evaluate to type int (default)
`$ cue eval -i numbers.cue`
```
a: 4
b: 4.0
c: _|_
b: 4
c: _|_ /* unsupported op &(int, float) */
d: 4
```
4 changes: 2 additions & 2 deletions doc/tutorial/basics/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ the same file.
The order in which files are loaded is undefined, but any order will result
in the same outcome, given that order does not matter.

<!-- CUE editor tab 1-->
<!-- CUE editor -->
_a.cue:_
```
package config
Expand All @@ -25,7 +25,7 @@ foo: 100
bar: int
```

<!-- CUE editor tab 2-->
<!-- CUE editor -->
_b.cue:_
```
package config
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial/basics/rangedef.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ word: int32
<!-- result -->
`$ cue eval -i range.cue`
```
a: _|_
a: _|_ /* -1 not within bound int & >=0 */
b: 128
c: 2000000000
```
6 changes: 3 additions & 3 deletions doc/tutorial/basics/ranges.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ rs: >="a" & <"mo"
`$ cue eval -i bounds.cue`
```
a: 3.5
b: _|_
c: 3.0
b: _|_ /* unsupported op &(int, float) */
c: 3
d: "ma"
e: _|_
e: _|_ /* "mu" not within bound <"mo" */
r1: >=5 & <8
```
6 changes: 2 additions & 4 deletions doc/tutorial/basics/regexp.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ e: "foo bar"
```
a: true
b: true
c: "^[a-z]{3}$"
c: =~"^[a-z]{3}$"
d: "foo"
e: _|_ // "foo bar" does not match =~"^[a-z]{3}$"
e: _|_ /* "foo bar" does not match =~"^[a-z]{3}$" */
```
89 changes: 89 additions & 0 deletions doc/tutorial/basics/tut_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.

package basics

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"cuelang.org/go/internal/cuetest"
)

func TestTutorial(t *testing.T) {
// t.Skip()

err := filepath.Walk(".", func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, ".md") {
t.Run(path, func(t *testing.T) { simulateFile(t, path) })
}
return nil
})
if err != nil {
t.Fatal(err)
}
}

func simulateFile(t *testing.T, path string) {
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("failed to open file %q: %v", path, err)
}

dir, err := ioutil.TempDir("", "tutbasics")
if err != nil {
t.Fatal(err)
}
defer os.Remove(dir)

c := cuetest.NewChunker(t, b)

// collect files
for c.Find("<!-- CUE editor -->") {
if !c.Next("_", "_") {
continue
}
filename := strings.TrimRight(c.Text(), ":")

if !c.Next("```", "```") {
t.Fatalf("No body for filename %q in file %q", filename, path)
}
b := bytes.TrimSpace(c.Bytes())

ioutil.WriteFile(filepath.Join(dir, filename), b, 0644)
}

if !c.Find("<!-- result -->") {
return
}

if !c.Next("`$ ", "`") {
t.Fatalf("No command for result section in file %q", path)
}
command := c.Text()

if !c.Next("```", "```") {
t.Fatalf("No body for result section in file %q", path)
}
gold := c.Text()
if p := strings.Index(gold, "\n"); p > 0 {
gold = gold[p+1:]
}

cuetest.Run(t, dir, command, gold)
}
12 changes: 6 additions & 6 deletions doc/tutorial/basics/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ explicitly.
_types.cue:_
```
point: {
x: float
y: float
x: number
y: number
}
xaxis: point
Expand All @@ -56,15 +56,15 @@ origin: xaxis & yaxis
`$ cue eval types.cue`
```
point: {
x: float
y: float
x: number
y: number
}
xaxis: {
x: 0
y: float
y: number
}
yaxis: {
x: float
x: number
y: 0
}
origin: {
Expand Down
34 changes: 27 additions & 7 deletions doc/tutorial/basics/unification.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,31 @@ s: c & b & a
<!-- result -->
`$ cue eval -i unification.cue`
```
a: { x: 1, y: 2 }
b: { y: 2, z: 3 }
c: { x: 1, z: 4 }
q: { x: 1, y: 2, z: _|_ }
r: { x: 1, y: 2, z: _|_ }
s: { x: 1, y: 2, z: _|_ }
a: {
x: 1
y: 2
}
b: {
y: 2
z: 3
}
c: {
x: 1
z: 4
}
q: {
x: 1
y: 2
z: _|_ /* conflicting values: 3 != 4 */
}
r: {
x: 1
y: 2
z: _|_ /* conflicting values: 3 != 4 */
}
s: {
x: 1
y: 2
z: _|_ /* conflicting values: 4 != 3 */
}
```
Loading

0 comments on commit 00c373d

Please sign in to comment.