1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #![cfg(feature = "supports-color")]
16
17 use googletest::prelude::*;
18 use indoc::indoc;
19 use std::fmt::{Display, Write};
20
21 // Make a long text with each element of the iterator on one line.
22 // `collection` must contains at least one element.
build_text<T: Display>(mut collection: impl Iterator<Item = T>) -> String23 fn build_text<T: Display>(mut collection: impl Iterator<Item = T>) -> String {
24 let mut text = String::new();
25 write!(&mut text, "{}", collection.next().expect("Provided collection without elements"))
26 .unwrap();
27 for item in collection {
28 write!(&mut text, "\n{}", item).unwrap();
29 }
30 text
31 }
32
33 #[test]
colors_suppressed_when_both_no_color_and_force_color_are_set() -> Result<()>34 fn colors_suppressed_when_both_no_color_and_force_color_are_set() -> Result<()> {
35 std::env::set_var("NO_COLOR", "1");
36 std::env::set_var("FORCE_COLOR", "1");
37
38 let result = verify_that!(build_text(1..50), eq(build_text(1..51)));
39
40 verify_that!(
41 result,
42 err(displays_as(contains_substring(indoc! {
43 "
44
45 Difference(-actual / +expected):
46 1
47 2
48 <---- 45 common lines omitted ---->
49 48
50 49
51 +50"
52 })))
53 )
54 }
55