Skip to content

Commit 05d4afd

Browse files
ankddevlpil
authored andcommitted
test: add tests and snapshots
1 parent 1125552 commit 05d4afd

5 files changed

+200
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "\n import gleam/list\n\n pub fn main() {\n let a_list = []\n let _ = 0 < list.length(a_list)\n }\n "
4+
---
5+
----- SOURCE CODE
6+
-- gleam/list.gleam
7+
pub fn length(_list: List(a)) -> Int { 0 }
8+
9+
-- main.gleam
10+
11+
import gleam/list
12+
13+
pub fn main() {
14+
let a_list = []
15+
let _ = 0 < list.length(a_list)
16+
}
17+
18+
19+
----- WARNING
20+
warning: Inefficient use of `list.length`
21+
┌─ /src/warning/wrn.gleam:6:21
22+
23+
6let _ = 0 < list.length(a_list)
24+
^^^^^^^^^^^^^^^^^^^^^^^
25+
26+
The `list.length` function has to iterate across the whole
27+
list to calculate the length, which is wasteful if you only
28+
need to know if the list is empty or not.
29+
30+
Hint: You can use `the_list != []` instead.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "\n import gleam/list\n\n pub fn main() {\n let a_list = []\n let _ = list.length(a_list) > 0\n }\n "
4+
---
5+
----- SOURCE CODE
6+
-- gleam/list.gleam
7+
pub fn length(_list: List(a)) -> Int { 0 }
8+
9+
-- main.gleam
10+
11+
import gleam/list
12+
13+
pub fn main() {
14+
let a_list = []
15+
let _ = list.length(a_list) > 0
16+
}
17+
18+
19+
----- WARNING
20+
warning: Inefficient use of `list.length`
21+
┌─ /src/warning/wrn.gleam:6:21
22+
23+
6let _ = list.length(a_list) > 0
24+
^^^^^^^^^^^^^^^^^^^^^^^
25+
26+
The `list.length` function has to iterate across the whole
27+
list to calculate the length, which is wasteful if you only
28+
need to know if the list is empty or not.
29+
30+
Hint: You can use `the_list != []` instead.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "\n import gleam/list\n\n pub fn main() {\n let a_list = []\n let _ = list.length(a_list) > 0\n }\n "
4+
---
5+
----- SOURCE CODE
6+
-- gleam/list.gleam
7+
pub fn length(_list: List(a)) -> Int { 0 }
8+
9+
-- main.gleam
10+
11+
import gleam/list
12+
13+
pub fn main() {
14+
let a_list = []
15+
let _ = list.length(a_list) > 0
16+
}
17+
18+
19+
----- WARNING
20+
warning: Inefficient use of `list.length`
21+
┌─ /src/warning/wrn.gleam:6:21
22+
23+
6let _ = list.length(a_list) > 0
24+
^^^^^^^^^^^^^^^^^^^^^^^
25+
26+
The `list.length` function has to iterate across the whole
27+
list to calculate the length, which is wasteful if you only
28+
need to know if the list is empty or not.
29+
30+
Hint: You can use `the_list != []` instead.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "\n import gleam/list\n\n pub fn main() {\n let a_list = []\n let _ = 0 < list.length(a_list)\n }\n "
4+
---
5+
----- SOURCE CODE
6+
-- gleam/list.gleam
7+
pub fn length(_list: List(a)) -> Int { 0 }
8+
9+
-- main.gleam
10+
11+
import gleam/list
12+
13+
pub fn main() {
14+
let a_list = []
15+
let _ = 0 < list.length(a_list)
16+
}
17+
18+
19+
----- WARNING
20+
warning: Inefficient use of `list.length`
21+
┌─ /src/warning/wrn.gleam:6:21
22+
23+
6let _ = 0 < list.length(a_list)
24+
^^^^^^^^^^^^^^^^^^^^^^^
25+
26+
The `list.length` function has to iterate across the whole
27+
list to calculate the length, which is wasteful if you only
28+
need to know if the list is empty or not.
29+
30+
Hint: You can use `the_list != []` instead.

compiler-core/src/type_/tests/warnings.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,86 @@ fn prefer_list_is_empty_over_list_length_lt_1() {
678678
);
679679
}
680680

681+
/// https://github.com/gleam-lang/gleam/issues/4861
682+
#[test]
683+
fn prefer_list_is_empty_over_list_length_gt_negative_0() {
684+
assert_warning!(
685+
(
686+
"gleam_stdlib",
687+
"gleam/list",
688+
"pub fn length(_list: List(a)) -> Int { 0 }"
689+
),
690+
r#"
691+
import gleam/list
692+
693+
pub fn main() {
694+
let a_list = []
695+
let _ = list.length(a_list) > 0
696+
}
697+
"#
698+
);
699+
}
700+
701+
/// https://github.com/gleam-lang/gleam/issues/4861
702+
#[test]
703+
fn prefer_list_is_empty_over_negative_0_lt_list_length() {
704+
assert_warning!(
705+
(
706+
"gleam_stdlib",
707+
"gleam/list",
708+
"pub fn length(_list: List(a)) -> Int { 0 }"
709+
),
710+
r#"
711+
import gleam/list
712+
713+
pub fn main() {
714+
let a_list = []
715+
let _ = 0 < list.length(a_list)
716+
}
717+
"#
718+
);
719+
}
720+
721+
/// https://github.com/gleam-lang/gleam/issues/4861
722+
#[test]
723+
fn prefer_list_is_empty_over_list_length_gt_0() {
724+
assert_warning!(
725+
(
726+
"gleam_stdlib",
727+
"gleam/list",
728+
"pub fn length(_list: List(a)) -> Int { 0 }"
729+
),
730+
r#"
731+
import gleam/list
732+
733+
pub fn main() {
734+
let a_list = []
735+
let _ = list.length(a_list) > 0
736+
}
737+
"#
738+
);
739+
}
740+
741+
/// https://github.com/gleam-lang/gleam/issues/4861
742+
#[test]
743+
fn prefer_list_is_empty_over_0_lt_list_length() {
744+
assert_warning!(
745+
(
746+
"gleam_stdlib",
747+
"gleam/list",
748+
"pub fn length(_list: List(a)) -> Int { 0 }"
749+
),
750+
r#"
751+
import gleam/list
752+
753+
pub fn main() {
754+
let a_list = []
755+
let _ = 0 < list.length(a_list)
756+
}
757+
"#
758+
);
759+
}
760+
681761
/// https://github.com/gleam-lang/gleam/issues/2067
682762
#[test]
683763
fn allow_list_length_eq_1() {

0 commit comments

Comments
 (0)