# Simple Calculator Project

### Module Objectives

* Creating a simple project
* Adding code
* Uploading our code to GitLab
* Writing test cases to check for failed and successful builds

### Creating a Project Repository

Now that we have everything set up, we can start making projects. Start by logging into your normal user (if you haven't already), then go to **Projects -> New Project.** *You can remove the README.*

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FhwxI5NGfh3VeUSCpzvi2%2Fimage.png?alt=media&#x26;token=6ed5dac5-c967-44c2-917d-da066ea4dc65" alt=""><figcaption><p>Set your project to public or private - we can use private for this one since we have our SSH key uploaded</p></figcaption></figure>

Once it's been created, git clone your project into a directory

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2Fq9D3bsmbhg2iq41pCOyi%2Fimage.png?alt=media&#x26;token=cf545d5c-03eb-43b6-b65c-335ccdbdaa5a" alt=""><figcaption><p>Cloning the project to our Windows host</p></figcaption></figure>

### Making a simple calculator

To test if our runner works, we will create a simple program which we can upload then run tests and configure pipelines for.

Let's make a simple calculator. I'll be using Go for this example

{% code title="calculator.go" %}

```go
package main
import "fmt"


func Add(x, y int) (res int) {
	return x + y
}


func Subtract(x, y int) (res int) {
	return x - y
}


func main() {
	fmt.Println("Addition: ", Add(1, 2))
	fmt.Println("Subtraction: ", Subtract(4, 2))
}
```

{% endcode %}

Test this by running `go run calculator.go`

```
Addition:  3
Subtraction:  2
```

Now that it works, we can push our demo app to our repository

```bash
> git switch -c main
> git add .
> git commit -m "Calculator Demo"
> git push origin main
```

<figure><img src="https://1797977785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjrIJ5xrJuOVgeeYdKNB5%2Fuploads%2FriuW5ExZZeBYkqT23ZlW%2Fimage.png?alt=media&#x26;token=577dfb41-fee7-429d-87b6-985584b5f629" alt=""><figcaption><p>Post-Push Repository</p></figcaption></figure>

### Writing Test Cases

Now that we have a functioning program, we can write a test case to check whether the values are acceptable, if not it will fail the build. We will reuse the code above for this

[*Reference*](https://www.digitalocean.com/community/tutorials/how-to-write-unit-tests-in-go-using-go-test-and-the-testing-package)

{% code title="calculator.go" %}

```go
package main
import "fmt"


func Add(x, y int) (res int) {
	return x + y
}


func Subtract(x, y int) (res int) {
	return x - y
}


func main() {
	fmt.Println("Addition: ", Add(1, 2))
	fmt.Println("Subtraction: ", Subtract(4, 2))
}
```

{% endcode %}

{% code title="calculator\_test.go" %}

```go
package main
import "testing"

func TestAdd(t *testing.T){
	got := Add(4, 4)
	want:= 8

	if got != want {
		t.Errorf("got %q. wanted %q", got, want)
	}
}


func TestSubtract(t *testing.T){
	got := Subtract(6, 4)
	want:= 2

	if got != want {
		t.Errorf("got %q. wanted %q", got, want)
	}
}

```

{% endcode %}

To run the test:

```bash
> go mod init calculator
> go test

# Result
> go test         
PASS
ok      calculator      0.127s
```

We can test for failure as well by changing the `want` variable to a random integer/string

```bash
> go test
--- FAIL: TestAdd (0.00s)
    calculator_test.go:9: got '\b'. wanted '\x05'
--- FAIL: TestSubtract (0.00s)
    calculator_test.go:19: got '\x02'. wanted '\x03'
FAIL
exit status 1
FAIL    calculator      0.215s
```

Change back the values and push the code to your repository
