Skip to content

Commit

Permalink
add tests for all the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Feb 1, 2024
1 parent 725c7b4 commit 7759018
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 73 deletions.
15 changes: 15 additions & 0 deletions birdie_snapshots/error_messages_in_a_big_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
version: 1.0.4
title: error messages in a big space
---
[ E001 ]: Unused function
1 | fn greet(message: String) -> Nil {
┬────
╰─ This function is unused!
You can safely remove it or make it public with the `pub` keyword.

[ E011 ]: Unknown variable
2 | println("Hello" <> message)
┬──────
╰─ The name `println` is not in scope here.
Did you mean to use `io.println`?
21 changes: 21 additions & 0 deletions birdie_snapshots/error_messages_in_a_small_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
version: 1.0.4
title: error messages in a small space
---
[ E001 ]: Unused function
1 | fn greet(message: String) -> Nil {
┬────
╰─ This function is
unused!
You can safely
remove it or make it
public with the
`pub` keyword.

[ E011 ]: Unknown variable
2 | println("Hello" <> message)
┬──────
╰─ The name `println` is
not in scope here.
Did you mean to use
`io.println`?
5 changes: 5 additions & 0 deletions birdie_snapshots/gleam_list_in_a_big_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
version: 1.0.4
title: gleam list in a big space
---
["Gleam", "is", "fun!"]
9 changes: 9 additions & 0 deletions birdie_snapshots/gleam_list_in_a_small_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
version: 1.0.4
title: gleam list in a small space
---
[
"Gleam",
"is",
"fun!",
]
13 changes: 13 additions & 0 deletions birdie_snapshots/json_in_a_big_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
version: 1.0.4
title: json in a big space
---
{
title: "The sundial",
author: "Shirley Jackson",
publication_year: 1958.0,
read: true,
characters: [ "Mrs. Halloran", "Essex", "Captain Scarabombardon" ],
average_rating: 5.0,
ratings: [ { from: "Ben", value: 5.0 }, { from: "Giacomo", value: 5.0 } ]
}
16 changes: 16 additions & 0 deletions birdie_snapshots/json_in_a_small_ish_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
version: 1.0.4
title: json in a small-ish space
---
{
title: "The sundial",
author: "Shirley Jackson",
publication_year: 1958.0,
read: true,
characters: [ "Mrs. Halloran", "Essex", "Captain Scarabombardon" ],
average_rating: 5.0,
ratings: [
{ from: "Ben", value: 5.0 },
{ from: "Giacomo", value: 5.0 }
]
}
26 changes: 26 additions & 0 deletions birdie_snapshots/json_in_a_small_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
version: 1.0.4
title: json in a small space
---
{
title: "The sundial",
author: "Shirley Jackson",
publication_year: 1958.0,
read: true,
characters: [
"Mrs. Halloran",
"Essex",
"Captain Scarabombardon"
],
average_rating: 5.0,
ratings: [
{
from: "Ben",
value: 5.0
},
{
from: "Giacomo",
value: 5.0
}
]
}
11 changes: 11 additions & 0 deletions birdie_snapshots/todo_list_in_a_big_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
version: 1.0.4
title: todo list in a big space
---
- […] publish Glam v1.1.0
- […] write a tutorial on todo lists
- […] add `doc.flex_break`
- [X] add the appropriate type variant
- [X] implement the missing cases
- [ ] add some tests
- [ ] get some sleep
11 changes: 11 additions & 0 deletions birdie_snapshots/todo_list_in_a_small_space.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
version: 1.0.4
title: todo list in a small space
---
- […] publish Glam v1.1.0
- […] write a tutorial on todo lists
- […] add `doc.flex_break`
- [X] add the appropriate type variant
- [X] implement the missing cases
- [ ] add some tests
- [ ] get some sleep
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gleam = ">= 0.33.0"

[dependencies]
gleam_stdlib = "~> 0.34 or ~> 1.0"
birdie = "~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
Expand Down
11 changes: 11 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
# You typically do not need to edit this file

packages = [
{ name = "argv", version = "1.0.1", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "A6E9009E50BBE863EB37D963E4315398D41A3D87D0075480FC244125808F964A" },
{ name = "birdie", version = "1.0.4", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "simplifile", "gleeunit", "gap", "justin", "gleam_stdlib", "filepath", "gleam_erlang", "argv", "rank"], otp_app = "birdie", source = "hex", outer_checksum = "0F7E16A3B12957B5B4A3B39152BD6D6175941AF40C1838F86C5A909DCFF7CF04" },
{ name = "filepath", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "534E8161A0DE192A9A105EFEC34369E9FD5834BB58ED449B5ACAEE8704358588" },
{ name = "gap", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_ansi"], otp_app = "gap", source = "hex", outer_checksum = "2EE1B0A17E85CF73A0C1D29DA315A2699117A8F549C8E8D89FA8261BE41EDEB1" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "A49A5E3AE8B637A5ACBA80ECB9B1AFE89FD3D5351FF6410A42B84F666D40D7D5" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
{ name = "rank", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rank", source = "hex", outer_checksum = "5660E361F0E49CBB714CC57CC4C89C63415D8986F05B2DA0C719D5642FAD91C9" },
{ name = "simplifile", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "AAFCF154F69B237D269FF2764890F61ABC4A7EF2A592D44D67627B99694539D9" },
]

[requirements]
birdie = { version = "~> 1.0"}
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
36 changes: 19 additions & 17 deletions src/examples/error_messages.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import gleam/list
import gleam/string
import glam/doc.{type Document}

type Span {
pub type Span {
Span(line: Int, column_start: Int, column_end: Int)
}

type Error {
pub type Error {
Error(code: String, name: String, message: String, span: Span)
}

/// Turns a list of errors into a Document that nicely display those with
/// a tooltip that highlights the error source.
///
///
/// The text of the error message has to be flexible: fitting as much as
/// possible on a single line and breaking it when needed in order not to exceed
/// the terminal's size.
///
///
/// The developer who's writing the error messages shouldn't worry about where
/// the error messages need to be split. Their only concern is dividing the
/// error text in (possibly) multiple paragraphs. The pretty printer is free to
/// split words inside paragraphs to fit them on a single line, but it should
/// never remove a newline where explicitly put by the original error message.
///
///
/// ## Examples
///
///
/// ```gleam
/// let source_code =
/// "fn greet(message: String) -> Nil {\n println(\"Hello\" <> message)\n}"
Expand All @@ -40,7 +40,7 @@ type Error {
/// errors_to_doc(source_code, errors)
/// |> doc.to_string(30)
/// |> io.println
///
///
/// // ->
/// // [ E001 ]: Unused function
/// // 1 | fn greet(message: String) -> Nil {
Expand All @@ -52,8 +52,8 @@ type Error {
/// // public with the
/// // `pub` keyword.
/// ```
///
fn errors_to_doc(source_code: String, errors: List(Error)) -> Document {
///
pub fn errors_to_doc(source_code: String, errors: List(Error)) -> Document {
list.map(errors, error_to_doc(source_code, _))
|> doc.join(with: doc.lines(2))
}
Expand Down Expand Up @@ -113,7 +113,7 @@ fn underlined_pointer(length: Int) -> Document {
/// where each whitespace in the original string is a `doc.flex_space`.
/// Original newlines are kept so that one can still have control on the
/// separation between different paragraphs.
///
///
fn flexible_text(text: String) -> Document {
let to_flexible_line = fn(line) {
string.split(line, on: " ")
Expand All @@ -128,10 +128,10 @@ fn flexible_text(text: String) -> Document {
|> doc.group
}

pub fn main() {
let source_code =
"fn greet(message: String) -> Nil {\n println(\"Hello\" <> message)\n}"
let errors = [
pub const example_source_code = "fn greet(message: String) -> Nil {\n println(\"Hello\" <> message)\n}"

pub fn example_errors() -> List(Error) {
[
Error(
"E001",
"Unused function",
Expand All @@ -145,11 +145,13 @@ pub fn main() {
Span(1, 2, 8),
),
]
}

errors_to_doc(source_code, errors)
pub fn main() {
errors_to_doc(example_source_code, example_errors())
|> doc.to_string(30)
|> io.println
// Example output:
// Example output:
//
// [ E001 ]: Unused function
// 1 | fn greet(message: String) -> Nil {
Expand All @@ -160,7 +162,7 @@ pub fn main() {
// remove it or make it
// public with the
// `pub` keyword.
//
//
// [ E011 ]: Unknown variable
// 2 | println("Hello" <> message)
// ┬──────
Expand Down
12 changes: 6 additions & 6 deletions src/examples/gleam_lists.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import glam/doc.{type Document}
/// - each string is separated by a comma and a space
/// - if the list does not fit on one line each one of its items should be on
/// a newline and indented
///
///
/// ## Examples
///
///
/// ```
/// let list = ["Gleam", "is", "fun!"] |> pretty_list
/// list |> doc.to_string(30)
/// // -> ["Gleam", "is", "fun!"]
///
///
/// list |> doc.to_string(6)
/// // -> [
/// // "Gleam",
/// // "is",
/// // "fun!",
/// // ]
/// ```
///
fn pretty_list(list: List(String)) -> Document {
///
pub fn pretty_list(list: List(String)) -> Document {
let list_item_to_document = fn(item) { doc.from_string("\"" <> item <> "\"") }
let comma = doc.concat([doc.from_string(","), doc.space])
let open_square = doc.concat([doc.from_string("["), doc.soft_break])
let trailing_comma = doc.break(",", "")
let trailing_comma = doc.break("", ",")
let close_square = doc.concat([trailing_comma, doc.from_string("]")])

list.map(list, list_item_to_document)
Expand Down
Loading

0 comments on commit 7759018

Please sign in to comment.