1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial |
3 | |
4 | //! Code to help with writing tests for the language server |
5 | |
6 | use lsp_types::{Diagnostic, Url}; |
7 | |
8 | use std::collections::HashMap; |
9 | |
10 | use crate::language::{reload_document_impl, DocumentCache}; |
11 | |
12 | /// Create an empty `DocumentCache` |
13 | pub fn empty_document_cache() -> DocumentCache { |
14 | let mut config: CompilerConfiguration = i_slint_compiler::CompilerConfiguration::new( |
15 | i_slint_compiler::generator::OutputFormat::Interpreter, |
16 | ); |
17 | config.style = Some("fluent" .to_string()); |
18 | DocumentCache::new(config) |
19 | } |
20 | |
21 | /// Create a `DocumentCache` with one document loaded into it. |
22 | pub fn loaded_document_cache( |
23 | content: String, |
24 | ) -> (DocumentCache, Url, HashMap<Url, Vec<Diagnostic>>) { |
25 | let mut dc: DocumentCache = empty_document_cache(); |
26 | |
27 | // Pre-load std-widgets.slint: |
28 | let mut diag: BuildDiagnostics = i_slint_compiler::diagnostics::BuildDiagnostics::default(); |
29 | spin_on::spin_on(future:dc.documents.import_component(file_to_import:"std-widgets.slint" , type_name:"StyleMetrics" , &mut diag)); |
30 | |
31 | let dummy_absolute_path: &str = |
32 | if cfg!(target_family = "windows" ) { "c://foo/bar.slint" } else { "/foo/bar.slint" }; |
33 | let url: Url = Url::from_file_path(dummy_absolute_path).unwrap(); |
34 | let diag: HashMap> = |
35 | spin_on::spin_on(future:reload_document_impl(ctx:None, content, url.clone(), version:Some(42), &mut dc)); |
36 | (dc, url, diag) |
37 | } |
38 | |
39 | /// Create a `DocumentCache` with one comparatively complex test document loaded into it. |
40 | pub fn complex_document_cache() -> (DocumentCache, Url, HashMap<Url, Vec<Diagnostic>>) { |
41 | loaded_document_cache( |
42 | r#"import { LineEdit, Button, Slider, HorizontalBox, VerticalBox } from "std-widgets.slint"; |
43 | |
44 | component MainWindow inherits Window { |
45 | property <duration> total-time: slider.value * 1s; |
46 | property <duration> elapsed-time; |
47 | |
48 | callback tick(duration); |
49 | tick(passed-time) => { |
50 | elapsed-time += passed-time; |
51 | elapsed-time = min(elapsed-time, total-time); |
52 | } |
53 | |
54 | VerticalBox { |
55 | HorizontalBox { |
56 | padding-left: 0; |
57 | Text { text: "Elapsed Time:"; } |
58 | Rectangle { |
59 | min-width: 200px; |
60 | max-height: 30px; |
61 | background: gray; |
62 | Rectangle { |
63 | height: 100%; |
64 | width: parent.width * (elapsed-time/total-time); |
65 | background: lightblue; |
66 | } |
67 | } |
68 | } |
69 | Text{ |
70 | text: (total-time / 1s) + "s"; |
71 | } |
72 | HorizontalBox { |
73 | padding-left: 0; |
74 | Text { |
75 | text: "Duration:"; |
76 | vertical-alignment: center; |
77 | } |
78 | slider := Slider { |
79 | maximum: 30s / 1s; |
80 | value: 10s / 1s; |
81 | changed(new-duration) => { |
82 | root.total-time = new-duration * 1s; |
83 | root.elapsed-time = min(root.elapsed-time, root.total-time); |
84 | } |
85 | } |
86 | } |
87 | Button { |
88 | text: "Reset"; |
89 | clicked => { |
90 | elapsed-time = 0 |
91 | } |
92 | } |
93 | } |
94 | } |
95 | "# .to_string()) |
96 | } |
97 | |