Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stdlib): sub operator; feat(e2e): more operator tests #791

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub def Greet(data string) (res string) { // new component
// src/main.neva
import {
fmt
@:utils // new import
@:src/utils // new import
}

def Main(start any) (stop any) {
Expand All @@ -313,7 +313,7 @@ def Main(start any) (stop any) {
Notice how we can have multiple imports:

- `fmt` from the standard library for printing
- `@:utils` from our local module (`@` is module name, `:` separates module/package)
- `@:src/utils` from our local module (`@` is module name, `:` separates module/package)

This modular structure keeps your code organized and reusable as your projects grow.

Expand Down Expand Up @@ -355,7 +355,7 @@ We can use `Greet` (import needed) and `AddExclamation` (no import needed) in ou
```neva
import {
fmt
@:utils
@:src/utils
}

def Main(start any) (stop any) {
Expand Down Expand Up @@ -441,7 +441,7 @@ When using nodes with multiple inports, we can't use chain syntax because compil
```neva
import {
fmt
@:utils
@:src/utils
}

def Main(start any) (stop any) {
Expand Down Expand Up @@ -473,7 +473,7 @@ Unlike self outports, we can ignore node outports we don't need (like `concat:de
```neva
import {
fmt
@:utils
@:src/utils
}

def Main(start any) (stop any) {
Expand Down Expand Up @@ -577,7 +577,7 @@ Now let's update `src/main.neva` to add `'21'` to itself using our new `utils.Ad
```neva
import {
fmt
@:utils
@:src/utils
}

def Main(start any) (stop any) {
Expand Down Expand Up @@ -677,7 +677,7 @@ All three operands can be any valid senders (ports, literals, constants, express
```neva
import {
fmt
@:utils
@:src/utils
}

def Main(start any) (stop any) {
Expand Down Expand Up @@ -808,7 +808,7 @@ Let's update `src/main.neva` to see how it can be used:
```neva
import {
fmt
@:utils
@:src/utils
}

def Main(start any) (stop any) {
Expand Down Expand Up @@ -856,7 +856,7 @@ Here's how it can be used in `src/main.neva`
```neva
import {
fmt
@:utils
@:src/utils
}

def Main(start any) (stop any) {
Expand Down
16 changes: 16 additions & 0 deletions e2e/binary_operators/arithmetic/add_floats/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
cmd := exec.Command("neva", "run", "main")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "42\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
7 changes: 7 additions & 0 deletions e2e/binary_operators/arithmetic/add_floats/main/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fmt }

def Main(start any) (stop any) {
println fmt.Println
---
:start -> { (21.5 + 20.5) -> println -> :stop }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fmt }

def Main(start any) (stop any) {
fmt.Println
println fmt.Println
---
:start -> { (5 + 3) -> println -> :stop }
}
16 changes: 16 additions & 0 deletions e2e/binary_operators/arithmetic/add_strings/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
cmd := exec.Command("neva", "run", "main")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "Hello, World!\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
7 changes: 7 additions & 0 deletions e2e/binary_operators/arithmetic/add_strings/main/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fmt }

def Main(start any) (stop any) {
println fmt.Println
---
:start -> { ('Hello, ' + 'World!') -> println -> :stop }
}
16 changes: 16 additions & 0 deletions e2e/binary_operators/arithmetic/divide_floats/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
cmd := exec.Command("neva", "run", "main")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "5\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
7 changes: 7 additions & 0 deletions e2e/binary_operators/arithmetic/divide_floats/main/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fmt }

def Main(start any) (stop any) {
fmt.Println
---
:start -> { (15.0 / 3.0) -> println -> :stop }
}
16 changes: 16 additions & 0 deletions e2e/binary_operators/arithmetic/multiply_floats/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
cmd := exec.Command("neva", "run", "main")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "18.15\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fmt }

def Main(start any) (stop any) {
fmt.Println
---
:start -> { (5.5 * 3.3) -> println -> :stop }
}
16 changes: 16 additions & 0 deletions e2e/binary_operators/arithmetic/subtract_floats/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
cmd := exec.Command("neva", "run", "main")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "1\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fmt }

def Main(start any) (stop any) {
println fmt.Println
---
:start -> { (21.5 - 20.5) -> println -> :stop }
}
1 change: 1 addition & 0 deletions e2e/binary_operators/arithmetic/subtract_ints/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.28.0
2 changes: 1 addition & 1 deletion e2e/binary_operators/comparison/equal/main/main.neva
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ def Main(start any) (stop any) {
fmt.Println
---
:start -> { (5 == 5) -> println -> :stop }
}
}
1 change: 1 addition & 0 deletions e2e/binary_operators/comparison/greater_int/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.28.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.28.0
1 change: 1 addition & 0 deletions e2e/binary_operators/comparison/less_int/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.28.0
1 change: 1 addition & 0 deletions e2e/binary_operators/comparison/less_or_equal_int/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.28.0
42 changes: 42 additions & 0 deletions examples/switch/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package test

import (
"os"
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
err := os.Chdir("..")
require.NoError(t, err)

wd, err := os.Getwd()
require.NoError(t, err)
defer os.Chdir(wd)

// Test successful case with "Alice"
cmd := exec.Command("neva", "run", "switch")
cmd.Stdin = strings.NewReader("Alice\n")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "Enter the name: ALICE\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())

// Test panic case with "Bob"
cmd = exec.Command("neva", "run", "switch")
cmd.Stdin = strings.NewReader("Bob\n")
out, err = cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "Enter the name: bob\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())

// Test panic case with "Charlie"
cmd = exec.Command("neva", "run", "switch")
cmd.Stdin = strings.NewReader("Charlie\n")
out, err = cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "Enter the name: panic: Charlie\n", string(out))
}
20 changes: 20 additions & 0 deletions examples/switch/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
fmt
strings
}

def Main(start any) (stop any) {
print fmt.Print
scanln fmt.Scanln
upper strings.ToUpper
lower strings.ToLower
println fmt.Println
panic Panic
---
:start -> 'Enter the name: ' -> print -> scanln -> switch {
'Alice' -> upper
'Bob' -> lower
_ -> panic
}
[upper, lower] -> println -> :stop
}
33 changes: 33 additions & 0 deletions examples/switch_fan_out/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package test

import (
"os"
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
err := os.Chdir("..")
require.NoError(t, err)

wd, err := os.Getwd()
require.NoError(t, err)
defer os.Chdir(wd)

// Test successful case with "Alice"
cmd := exec.Command("neva", "run", "switch_fan_out")
cmd.Stdin = strings.NewReader("Alice\n")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(t, "Enter the name: ALICEalice\n", string(out))
require.Equal(t, 0, cmd.ProcessState.ExitCode())

// Test panic case with "Bob"
cmd = exec.Command("neva", "run", "switch_fan_out")
cmd.Stdin = strings.NewReader("Bob\n")
out, _ = cmd.CombinedOutput()
require.Equal(t, "Enter the name: panic: Bob\n", string(out))
}
19 changes: 19 additions & 0 deletions examples/switch_fan_out/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
fmt
strings
}

def Main(start any) (stop any) {
print fmt.Print
scanln fmt.Scanln
upper strings.ToUpper
lower strings.ToLower
println fmt.Println
panic Panic
---
:start -> 'Enter the name: ' -> print -> scanln -> switch {
'Alice' -> [upper, lower]
_ -> panic
}
(upper + lower) -> println -> :stop
}
11 changes: 3 additions & 8 deletions internal/compiler/analyzer/main_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (a Analyzer) analyzeMainComponent(cmp src.Component, scope src.Scope) *comp
return compiler.Error{Meta: &cmp.Interface.Meta}.Wrap(err)
}

if err := a.analyzeMainFlowNodes(cmp.Nodes, scope); err != nil {
if err := a.analyzeMainComponentNodes(cmp.Nodes, scope); err != nil {
return compiler.Error{Meta: &cmp.Meta}.Wrap(err)
}

Expand Down Expand Up @@ -75,20 +75,15 @@ func (a Analyzer) analyzeMainComponentPort(port src.Port) *compiler.Error {
return nil
}

func (Analyzer) analyzeMainFlowNodes(
func (Analyzer) analyzeMainComponentNodes(
nodes map[string]src.Node,
scope src.Scope,
) *compiler.Error {
for nodeName, node := range nodes {
nodeEntity, loc, err := scope.Entity(node.EntityRef)
if err != nil {
return &compiler.Error{
Message: fmt.Sprintf(
"Referenced entity not found: node '%v', ref '%v', details '%v'",
nodeName,
node.EntityRef,
err,
),
Message: err.Error(),
Location: scope.Location(),
Meta: &node.EntityRef.Meta,
}
Expand Down
Loading
Loading