Move from learn-go-with-tests
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
18bde5d382
commit
b7c3fffbaa
10 changed files with 213 additions and 0 deletions
1
learn-go-with-tests/.envrc
Normal file
1
learn-go-with-tests/.envrc
Normal file
|
|
@ -0,0 +1 @@
|
|||
use flake
|
||||
7
learn-go-with-tests/.forgejo/workflows/test.yml
Normal file
7
learn-go-with-tests/.forgejo/workflows/test.yml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
on: push
|
||||
jobs:
|
||||
check:
|
||||
runs-on: nixos
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: nix develop -c go test
|
||||
5
learn-go-with-tests/README.md
Normal file
5
learn-go-with-tests/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# learn-go-with-tests
|
||||
|
||||
Working my way through [Learn Go with Tests](https://quii.gitbook.io/learn-go-with-tests).
|
||||
|
||||
Thanks, [@bashbunni](https://github.com/bashbunni), for the recommendation.
|
||||
18
learn-go-with-tests/flake-modules/dev-shell.nix
Normal file
18
learn-go-with-tests/flake-modules/dev-shell.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{ inputs, ... }:
|
||||
|
||||
{
|
||||
imports = [
|
||||
inputs.devshell.flakeModule
|
||||
];
|
||||
|
||||
perSystem =
|
||||
{ pkgs, ... }:
|
||||
{
|
||||
devshells.default = {
|
||||
packages = with pkgs; [
|
||||
go
|
||||
gopls
|
||||
];
|
||||
};
|
||||
};
|
||||
}
|
||||
3
learn-go-with-tests/flake-modules/systems.nix
Normal file
3
learn-go-with-tests/flake-modules/systems.nix
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
systems = [ "x86_64-linux" ];
|
||||
}
|
||||
98
learn-go-with-tests/flake.lock
generated
Normal file
98
learn-go-with-tests/flake.lock
generated
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
{
|
||||
"nodes": {
|
||||
"devshell": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1741473158,
|
||||
"narHash": "sha256-kWNaq6wQUbUMlPgw8Y+9/9wP0F8SHkjy24/mN3UAppg=",
|
||||
"owner": "numtide",
|
||||
"repo": "devshell",
|
||||
"rev": "7c9e793ebe66bcba8292989a68c0419b737a22a0",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "devshell",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-parts": {
|
||||
"inputs": {
|
||||
"nixpkgs-lib": "nixpkgs-lib"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1753121425,
|
||||
"narHash": "sha256-TVcTNvOeWWk1DXljFxVRp+E0tzG1LhrVjOGGoMHuXio=",
|
||||
"owner": "hercules-ci",
|
||||
"repo": "flake-parts",
|
||||
"rev": "644e0fc48951a860279da645ba77fe4a6e814c5e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "hercules-ci",
|
||||
"repo": "flake-parts",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"import-tree": {
|
||||
"locked": {
|
||||
"lastModified": 1752730890,
|
||||
"narHash": "sha256-GES8fapSLGz36MMPRVNkSUWXUTtqvGQNXHjRmRLfJUY=",
|
||||
"owner": "vic",
|
||||
"repo": "import-tree",
|
||||
"rev": "6ebb8cb87987b20264c09296166543fd3761d274",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "vic",
|
||||
"repo": "import-tree",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1753694789,
|
||||
"narHash": "sha256-cKgvtz6fKuK1Xr5LQW/zOUiAC0oSQoA9nOISB0pJZqM=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "dc9637876d0dcc8c9e5e22986b857632effeb727",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs-lib": {
|
||||
"locked": {
|
||||
"lastModified": 1751159883,
|
||||
"narHash": "sha256-urW/Ylk9FIfvXfliA1ywh75yszAbiTEVgpPeinFyVZo=",
|
||||
"owner": "nix-community",
|
||||
"repo": "nixpkgs.lib",
|
||||
"rev": "14a40a1d7fb9afa4739275ac642ed7301a9ba1ab",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-community",
|
||||
"repo": "nixpkgs.lib",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"devshell": "devshell",
|
||||
"flake-parts": "flake-parts",
|
||||
"import-tree": "import-tree",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
15
learn-go-with-tests/flake.nix
Normal file
15
learn-go-with-tests/flake.nix
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
inputs = {
|
||||
devshell.inputs.nixpkgs.follows = "nixpkgs";
|
||||
devshell.url = "github:numtide/devshell";
|
||||
flake-parts.url = "github:hercules-ci/flake-parts";
|
||||
import-tree.url = "github:vic/import-tree";
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
};
|
||||
|
||||
outputs =
|
||||
inputs:
|
||||
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
|
||||
imports = [ (inputs.import-tree ./flake-modules) ];
|
||||
};
|
||||
}
|
||||
3
learn-go-with-tests/go.mod
Normal file
3
learn-go-with-tests/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module hello
|
||||
|
||||
go 1.17
|
||||
29
learn-go-with-tests/hello.go
Normal file
29
learn-go-with-tests/hello.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
const englishHelloPrefix = "Hello, "
|
||||
const welsh = "Welsh"
|
||||
const welshHelloPrefix = "Helo, "
|
||||
|
||||
func Hello(name string, language string) string {
|
||||
if name == "" {
|
||||
name = "World"
|
||||
}
|
||||
|
||||
return greetingPrefix(language) + name
|
||||
}
|
||||
|
||||
func greetingPrefix(language string) (prefix string) {
|
||||
prefix = englishHelloPrefix
|
||||
|
||||
if language == welsh {
|
||||
prefix = welshHelloPrefix
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(Hello("Oliver", ""))
|
||||
}
|
||||
34
learn-go-with-tests/hello_test.go
Normal file
34
learn-go-with-tests/hello_test.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHello(t *testing.T) {
|
||||
assertCorrectMessage := func(t testing.TB, got string, want string) {
|
||||
t.Helper()
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("saying hello to people", func (t *testing.T) {
|
||||
got := Hello("Oliver", "")
|
||||
want := "Hello, Oliver"
|
||||
|
||||
assertCorrectMessage(t, got, want)
|
||||
})
|
||||
|
||||
t.Run("say 'Hello, World' when an empty string is supplied", func (t *testing.T) {
|
||||
got := Hello("", "")
|
||||
want := "Hello, World"
|
||||
|
||||
assertCorrectMessage(t, got, want)
|
||||
})
|
||||
|
||||
t.Run("in Welsh", func (t *testing.T) {
|
||||
got := Hello("Oliver", "Welsh")
|
||||
want := "Helo, Oliver"
|
||||
|
||||
assertCorrectMessage(t, got, want)
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue