microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/compiler/qsc_doc_gen/src/generate_docs/tests.rs
209lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use super::generate_docs; |
| 5 | use expect_test::expect; |
| 6 | |
| 7 | #[test] |
| 8 | fn generates_standard_item() { |
| 9 | let files = generate_docs(None, None, None); |
| 10 | let (_, metadata, contents) = files |
| 11 | .iter() |
| 12 | .find(|(file_name, _, _)| &**file_name == "Std.Core/Length.md") |
| 13 | .expect("Could not find doc file for Length"); |
| 14 | let full_contents = format!("{metadata}\n\n{contents}"); |
| 15 | |
| 16 | expect![[r#" |
| 17 | --- |
| 18 | uid: Qdk.Std.Core.Length |
| 19 | title: Length function |
| 20 | description: "Q# Length function: Returns the number of elements in the input array `a`." |
| 21 | ms.date: {TIMESTAMP} |
| 22 | qsharp.kind: function |
| 23 | qsharp.package: __Core__ |
| 24 | qsharp.namespace: Std.Core |
| 25 | qsharp.name: Length |
| 26 | qsharp.summary: "Returns the number of elements in the input array `a`." |
| 27 | --- |
| 28 | |
| 29 | # Length function |
| 30 | |
| 31 | Fully qualified name: Std.Core.Length |
| 32 | |
| 33 | ```qsharp |
| 34 | function Length<'T>(a : 'T[]) : Int |
| 35 | ``` |
| 36 | |
| 37 | ## Summary |
| 38 | Returns the number of elements in the input array `a`. |
| 39 | |
| 40 | ## Input |
| 41 | ### a |
| 42 | Input array. |
| 43 | |
| 44 | ## Output |
| 45 | The total number of elements in the input array `a`. |
| 46 | |
| 47 | ## Example |
| 48 | ```qsharp |
| 49 | Message($"{ Length([0, 0, 0]) }"); // Prints 3 |
| 50 | ``` |
| 51 | "#]] |
| 52 | .assert_eq(full_contents.as_str()); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn generates_unrestricted_item() { |
| 57 | let files = generate_docs(None, None, None); |
| 58 | let (_, metadata, contents) = files |
| 59 | .iter() |
| 60 | .find(|(file_name, _, _)| &**file_name == "Std.Diagnostics/CheckZero.md") |
| 61 | .expect("Could not file doc file for CheckZero"); |
| 62 | let full_contents = format!("{metadata}\n\n{contents}"); |
| 63 | |
| 64 | expect![[r#" |
| 65 | --- |
| 66 | uid: Qdk.Std.Diagnostics.CheckZero |
| 67 | title: CheckZero operation |
| 68 | description: "Q# CheckZero operation: Checks whether a qubit is in the |0⟩ state, returning true if it is." |
| 69 | ms.date: {TIMESTAMP} |
| 70 | qsharp.kind: operation |
| 71 | qsharp.package: __Std__ |
| 72 | qsharp.namespace: Std.Diagnostics |
| 73 | qsharp.name: CheckZero |
| 74 | qsharp.summary: "Checks whether a qubit is in the |0⟩ state, returning true if it is." |
| 75 | --- |
| 76 | |
| 77 | # CheckZero operation |
| 78 | |
| 79 | Fully qualified name: Std.Diagnostics.CheckZero |
| 80 | |
| 81 | ```qsharp |
| 82 | operation CheckZero(qubit : Qubit) : Bool |
| 83 | ``` |
| 84 | |
| 85 | ## Summary |
| 86 | Checks whether a qubit is in the |0⟩ state, returning true if it is. |
| 87 | |
| 88 | ## Description |
| 89 | This operation checks whether a qubit is in the |0⟩ state. It will return true only |
| 90 | if the qubit is deterministically in the |0⟩ state, and will return false otherwise. This operation |
| 91 | does not change the state of the qubit. |
| 92 | |
| 93 | ## Input |
| 94 | ### qubit |
| 95 | The qubit to check. |
| 96 | ## Output |
| 97 | True if the qubit is in the |0⟩ state, false otherwise. |
| 98 | |
| 99 | ## Remarks |
| 100 | This operation is useful for checking whether a qubit is in the |0⟩ state during simulation. It is not possible to check |
| 101 | this on hardware without measuring the qubit, which could change the state. |
| 102 | "#]] |
| 103 | .assert_eq(full_contents.as_str()); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn redirect_generation() { |
| 108 | let files = generate_docs(None, None, None); |
| 109 | let (_, metadata, contents) = files |
| 110 | .iter() |
| 111 | .find(|(file_name, _, _)| &**file_name == "Microsoft.Quantum.Core/Length.md") |
| 112 | .expect("Could not find doc file for Length"); |
| 113 | let full_contents = format!("{metadata}\n\n{contents}"); |
| 114 | |
| 115 | expect" |
| 120 | ms.date: {TIMESTAMP} |
| 121 | qsharp.kind: export |
| 122 | qsharp.package: __Std__ |
| 123 | qsharp.namespace: Microsoft.Quantum.Core |
| 124 | qsharp.name: Length |
| 125 | qsharp.summary: "This is an exported item. The actual definition is found here: [Std.Core.Length](xref:Qdk.Std.Core.Length)" |
| 126 | --- |
| 127 | |
| 128 | # Length exported item |
| 129 | |
| 130 | Fully qualified name: Microsoft.Quantum.Core.Length |
| 131 | |
| 132 | This is an exported item. The actual definition is found here: [Std.Core.Length](xref:Qdk.Std.Core.Length) |
| 133 | "#]] |
| 134 | .assert_eq(full_contents.as_str()); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn index_file_generation() { |
| 139 | let files = generate_docs(None, None, None); |
| 140 | let (_, metadata, contents) = files |
| 141 | .iter() |
| 142 | .find(|(file_name, _, _)| &**file_name == "Std.Core/index.md") |
| 143 | .expect("Could not find Std.Core Table of Contents file"); |
| 144 | let full_contents = format!("{metadata}\n\n{contents}"); |
| 145 | |
| 146 | expect | Returns the number of elements in the input array `a`. | |
| 162 | | [Repeated](xref:Qdk.Std.Core.Repeated) | Creates an array of given `length` with all elements equal to given `value`. `length` must be a non-negative integer. | |
| 163 | "#]] |
| 164 | .assert_eq(full_contents.as_str()); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn top_index_file_generation() { |
| 169 | let files = generate_docs(None, None, None); |
| 170 | let (_, metadata, contents) = files |
| 171 | .iter() |
| 172 | .find(|(file_name, _, _)| &**file_name == "index.md") |
| 173 | .expect("Could not find top-level Table of Contents file"); |
| 174 | let full_contents = format!("{metadata}\n\n{contents}"); |
| 175 | |
| 176 | expect | Re-exported functions. | |
| 192 | | [`Std.Arithmetic`](xref:Qdk.Std.Arithmetic-toc) | Items for working with quantum arithmetic operations. | |
| 193 | | [`Std.Arrays`](xref:Qdk.Std.Arrays-toc) | Items for working with arrays. | |
| 194 | | [`Std.Canon`](xref:Qdk.Std.Canon-toc) | Canonical implementations of common classical and quantum utilities. | |
| 195 | | [`Std.Convert`](xref:Qdk.Std.Convert-toc) | Items for converting between different types. | |
| 196 | | [`Std.Core`](xref:Qdk.Std.Core-toc) | Items for language built-in operations. | |
| 197 | | [`Std.Diagnostics`](xref:Qdk.Std.Diagnostics-toc) | Items for debugging and testing quantum programs. | |
| 198 | | [`Std.Intrinsic`](xref:Qdk.Std.Intrinsic-toc) | Items that provide core quantum operations. | |
| 199 | | [`Std.Logical`](xref:Qdk.Std.Logical-toc) | Boolean Logic functions. | |
| 200 | | [`Std.Math`](xref:Qdk.Std.Math-toc) | Items for classical math operations. | |
| 201 | | [`Std.Measurement`](xref:Qdk.Std.Measurement-toc) | Items for measuring quantum results. | |
| 202 | | [`Std.Random`](xref:Qdk.Std.Random-toc) | Items for creating random values. | |
| 203 | | [`Std.Range`](xref:Qdk.Std.Range-toc) | Items for working with ranges. | |
| 204 | | [`Std.ResourceEstimation`](xref:Qdk.Std.ResourceEstimation-toc) | Items for working with the Azure Quantum Resource Estimator. | |
| 205 | | [`Std.StatePreparation`](xref:Qdk.Std.StatePreparation-toc) | Items for preparing a quantum state. | |
| 206 | | [`Std.TableLookup`](xref:Qdk.Std.TableLookup-toc) | Items for performing quantum table lookups. | |
| 207 | "#]] |
| 208 | .assert_eq(full_contents.as_str()); |
| 209 | } |
| 210 | |