Skip to content

Commit 7759018

Browse files
add tests for all the examples
1 parent 725c7b4 commit 7759018

16 files changed

+286
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
version: 1.0.4
3+
title: error messages in a big space
4+
---
5+
[ E001 ]: Unused function
6+
1 | fn greet(message: String) -> Nil {
7+
┬────
8+
╰─ This function is unused!
9+
You can safely remove it or make it public with the `pub` keyword.
10+
11+
[ E011 ]: Unknown variable
12+
2 | println("Hello" <> message)
13+
┬──────
14+
╰─ The name `println` is not in scope here.
15+
Did you mean to use `io.println`?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
version: 1.0.4
3+
title: error messages in a small space
4+
---
5+
[ E001 ]: Unused function
6+
1 | fn greet(message: String) -> Nil {
7+
┬────
8+
╰─ This function is
9+
unused!
10+
You can safely
11+
remove it or make it
12+
public with the
13+
`pub` keyword.
14+
15+
[ E011 ]: Unknown variable
16+
2 | println("Hello" <> message)
17+
┬──────
18+
╰─ The name `println` is
19+
not in scope here.
20+
Did you mean to use
21+
`io.println`?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.0.4
3+
title: gleam list in a big space
4+
---
5+
["Gleam", "is", "fun!"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
version: 1.0.4
3+
title: gleam list in a small space
4+
---
5+
[
6+
"Gleam",
7+
"is",
8+
"fun!",
9+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
version: 1.0.4
3+
title: json in a big space
4+
---
5+
{
6+
title: "The sundial",
7+
author: "Shirley Jackson",
8+
publication_year: 1958.0,
9+
read: true,
10+
characters: [ "Mrs. Halloran", "Essex", "Captain Scarabombardon" ],
11+
average_rating: 5.0,
12+
ratings: [ { from: "Ben", value: 5.0 }, { from: "Giacomo", value: 5.0 } ]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
version: 1.0.4
3+
title: json in a small-ish space
4+
---
5+
{
6+
title: "The sundial",
7+
author: "Shirley Jackson",
8+
publication_year: 1958.0,
9+
read: true,
10+
characters: [ "Mrs. Halloran", "Essex", "Captain Scarabombardon" ],
11+
average_rating: 5.0,
12+
ratings: [
13+
{ from: "Ben", value: 5.0 },
14+
{ from: "Giacomo", value: 5.0 }
15+
]
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
version: 1.0.4
3+
title: json in a small space
4+
---
5+
{
6+
title: "The sundial",
7+
author: "Shirley Jackson",
8+
publication_year: 1958.0,
9+
read: true,
10+
characters: [
11+
"Mrs. Halloran",
12+
"Essex",
13+
"Captain Scarabombardon"
14+
],
15+
average_rating: 5.0,
16+
ratings: [
17+
{
18+
from: "Ben",
19+
value: 5.0
20+
},
21+
{
22+
from: "Giacomo",
23+
value: 5.0
24+
}
25+
]
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
version: 1.0.4
3+
title: todo list in a big space
4+
---
5+
- […] publish Glam v1.1.0
6+
- […] write a tutorial on todo lists
7+
- […] add `doc.flex_break`
8+
- [X] add the appropriate type variant
9+
- [X] implement the missing cases
10+
- [ ] add some tests
11+
- [ ] get some sleep
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
version: 1.0.4
3+
title: todo list in a small space
4+
---
5+
- […] publish Glam v1.1.0
6+
- […] write a tutorial on todo lists
7+
- […] add `doc.flex_break`
8+
- [X] add the appropriate type variant
9+
- [X] implement the missing cases
10+
- [ ] add some tests
11+
- [ ] get some sleep

gleam.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ gleam = ">= 0.33.0"
99

1010
[dependencies]
1111
gleam_stdlib = "~> 0.34 or ~> 1.0"
12+
birdie = "~> 1.0"
1213

1314
[dev-dependencies]
1415
gleeunit = "~> 1.0"

manifest.toml

+11
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22
# You typically do not need to edit this file
33

44
packages = [
5+
{ name = "argv", version = "1.0.1", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "A6E9009E50BBE863EB37D963E4315398D41A3D87D0075480FC244125808F964A" },
6+
{ 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" },
7+
{ name = "filepath", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "534E8161A0DE192A9A105EFEC34369E9FD5834BB58ED449B5ACAEE8704358588" },
8+
{ name = "gap", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_ansi"], otp_app = "gap", source = "hex", outer_checksum = "2EE1B0A17E85CF73A0C1D29DA315A2699117A8F549C8E8D89FA8261BE41EDEB1" },
9+
{ 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" },
10+
{ name = "gleam_community_colour", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "A49A5E3AE8B637A5ACBA80ECB9B1AFE89FD3D5351FF6410A42B84F666D40D7D5" },
11+
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
512
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
613
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
14+
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
15+
{ name = "rank", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rank", source = "hex", outer_checksum = "5660E361F0E49CBB714CC57CC4C89C63415D8986F05B2DA0C719D5642FAD91C9" },
16+
{ name = "simplifile", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "AAFCF154F69B237D269FF2764890F61ABC4A7EF2A592D44D67627B99694539D9" },
717
]
818

919
[requirements]
20+
birdie = { version = "~> 1.0"}
1021
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
1122
gleeunit = { version = "~> 1.0" }

src/examples/error_messages.gleam

+19-17
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ import gleam/list
44
import gleam/string
55
import glam/doc.{type Document}
66

7-
type Span {
7+
pub type Span {
88
Span(line: Int, column_start: Int, column_end: Int)
99
}
1010

11-
type Error {
11+
pub type Error {
1212
Error(code: String, name: String, message: String, span: Span)
1313
}
1414

1515
/// Turns a list of errors into a Document that nicely display those with
1616
/// a tooltip that highlights the error source.
17-
///
17+
///
1818
/// The text of the error message has to be flexible: fitting as much as
1919
/// possible on a single line and breaking it when needed in order not to exceed
2020
/// the terminal's size.
21-
///
21+
///
2222
/// The developer who's writing the error messages shouldn't worry about where
2323
/// the error messages need to be split. Their only concern is dividing the
2424
/// error text in (possibly) multiple paragraphs. The pretty printer is free to
2525
/// split words inside paragraphs to fit them on a single line, but it should
2626
/// never remove a newline where explicitly put by the original error message.
27-
///
27+
///
2828
/// ## Examples
29-
///
29+
///
3030
/// ```gleam
3131
/// let source_code =
3232
/// "fn greet(message: String) -> Nil {\n println(\"Hello\" <> message)\n}"
@@ -40,7 +40,7 @@ type Error {
4040
/// errors_to_doc(source_code, errors)
4141
/// |> doc.to_string(30)
4242
/// |> io.println
43-
///
43+
///
4444
/// // ->
4545
/// // [ E001 ]: Unused function
4646
/// // 1 | fn greet(message: String) -> Nil {
@@ -52,8 +52,8 @@ type Error {
5252
/// // public with the
5353
/// // `pub` keyword.
5454
/// ```
55-
///
56-
fn errors_to_doc(source_code: String, errors: List(Error)) -> Document {
55+
///
56+
pub fn errors_to_doc(source_code: String, errors: List(Error)) -> Document {
5757
list.map(errors, error_to_doc(source_code, _))
5858
|> doc.join(with: doc.lines(2))
5959
}
@@ -113,7 +113,7 @@ fn underlined_pointer(length: Int) -> Document {
113113
/// where each whitespace in the original string is a `doc.flex_space`.
114114
/// Original newlines are kept so that one can still have control on the
115115
/// separation between different paragraphs.
116-
///
116+
///
117117
fn flexible_text(text: String) -> Document {
118118
let to_flexible_line = fn(line) {
119119
string.split(line, on: " ")
@@ -128,10 +128,10 @@ fn flexible_text(text: String) -> Document {
128128
|> doc.group
129129
}
130130

131-
pub fn main() {
132-
let source_code =
133-
"fn greet(message: String) -> Nil {\n println(\"Hello\" <> message)\n}"
134-
let errors = [
131+
pub const example_source_code = "fn greet(message: String) -> Nil {\n println(\"Hello\" <> message)\n}"
132+
133+
pub fn example_errors() -> List(Error) {
134+
[
135135
Error(
136136
"E001",
137137
"Unused function",
@@ -145,11 +145,13 @@ pub fn main() {
145145
Span(1, 2, 8),
146146
),
147147
]
148+
}
148149

149-
errors_to_doc(source_code, errors)
150+
pub fn main() {
151+
errors_to_doc(example_source_code, example_errors())
150152
|> doc.to_string(30)
151153
|> io.println
152-
// Example output:
154+
// Example output:
153155
//
154156
// [ E001 ]: Unused function
155157
// 1 | fn greet(message: String) -> Nil {
@@ -160,7 +162,7 @@ pub fn main() {
160162
// remove it or make it
161163
// public with the
162164
// `pub` keyword.
163-
//
165+
//
164166
// [ E011 ]: Unknown variable
165167
// 2 | println("Hello" <> message)
166168
// ┬──────

src/examples/gleam_lists.gleam

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ import glam/doc.{type Document}
99
/// - each string is separated by a comma and a space
1010
/// - if the list does not fit on one line each one of its items should be on
1111
/// a newline and indented
12-
///
12+
///
1313
/// ## Examples
14-
///
14+
///
1515
/// ```
1616
/// let list = ["Gleam", "is", "fun!"] |> pretty_list
1717
/// list |> doc.to_string(30)
1818
/// // -> ["Gleam", "is", "fun!"]
19-
///
19+
///
2020
/// list |> doc.to_string(6)
2121
/// // -> [
2222
/// // "Gleam",
2323
/// // "is",
2424
/// // "fun!",
2525
/// // ]
2626
/// ```
27-
///
28-
fn pretty_list(list: List(String)) -> Document {
27+
///
28+
pub fn pretty_list(list: List(String)) -> Document {
2929
let list_item_to_document = fn(item) { doc.from_string("\"" <> item <> "\"") }
3030
let comma = doc.concat([doc.from_string(","), doc.space])
3131
let open_square = doc.concat([doc.from_string("["), doc.soft_break])
32-
let trailing_comma = doc.break(",", "")
32+
let trailing_comma = doc.break("", ",")
3333
let close_square = doc.concat([trailing_comma, doc.from_string("]")])
3434

3535
list.map(list, list_item_to_document)

0 commit comments

Comments
 (0)