microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
compiler/qsc_frontend/src/compile/preprocess/tests.rs
134lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use qsc_ast::ast::{Attr, Expr, ExprKind, Ident, NodeId, Path, PathKind}; |
| 5 | use qsc_data_structures::span::Span; |
| 6 | |
| 7 | use crate::compile::{preprocess::matches_config, TargetCapabilityFlags}; |
| 8 | |
| 9 | fn named_attr(name: &str) -> Attr { |
| 10 | Attr { |
| 11 | name: Box::new(Ident { |
| 12 | name: name.into(), |
| 13 | span: Span::default(), |
| 14 | id: NodeId::default(), |
| 15 | }), |
| 16 | arg: Box::new(Expr { |
| 17 | id: NodeId::default(), |
| 18 | span: Span::default(), |
| 19 | kind: Box::new(ExprKind::Tuple(Box::new([]))), |
| 20 | }), |
| 21 | span: Span::default(), |
| 22 | id: NodeId::default(), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn name_value_attr(name: &str, value: &str) -> Attr { |
| 27 | Attr { |
| 28 | name: Box::new(Ident { |
| 29 | name: name.into(), |
| 30 | span: Span::default(), |
| 31 | id: NodeId::default(), |
| 32 | }), |
| 33 | arg: Box::new(Expr { |
| 34 | id: NodeId::default(), |
| 35 | span: Span::default(), |
| 36 | kind: Box::new(ExprKind::Paren(Box::new(Expr { |
| 37 | id: NodeId::default(), |
| 38 | span: Span::default(), |
| 39 | kind: Box::new(ExprKind::Path(PathKind::Ok(Box::new(Path { |
| 40 | id: NodeId::default(), |
| 41 | span: Span::default(), |
| 42 | segments: None, |
| 43 | name: Box::new(Ident { |
| 44 | name: value.into(), |
| 45 | span: Span::default(), |
| 46 | id: NodeId::default(), |
| 47 | }), |
| 48 | })))), |
| 49 | }))), |
| 50 | }), |
| 51 | span: Span::default(), |
| 52 | id: NodeId::default(), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn no_attrs_matches() { |
| 58 | assert!(matches_config(&[], TargetCapabilityFlags::empty())); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn unknown_attrs_matches() { |
| 63 | assert!(matches_config( |
| 64 | &[Box::new(named_attr("unknown"))], |
| 65 | TargetCapabilityFlags::empty() |
| 66 | )); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn none_attrs_matches_empty() { |
| 71 | assert!(matches_config( |
| 72 | &[Box::new(name_value_attr("Config", "Base"))], |
| 73 | TargetCapabilityFlags::empty() |
| 74 | )); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn none_attrs_does_not_match_all() { |
| 79 | assert!(!matches_config( |
| 80 | &[Box::new(name_value_attr("Config", "Base"))], |
| 81 | TargetCapabilityFlags::all() |
| 82 | )); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn none_attrs_does_not_match_adaptive() { |
| 87 | assert!(!matches_config( |
| 88 | &[Box::new(name_value_attr("Config", "Base"))], |
| 89 | TargetCapabilityFlags::Adaptive |
| 90 | )); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn adaptive_attrs_does_not_match_empty() { |
| 95 | assert!(!matches_config( |
| 96 | &[Box::new(name_value_attr("Config", "Adaptive"))], |
| 97 | TargetCapabilityFlags::empty() |
| 98 | )); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn integercomputations_attrs_does_not_match_empty() { |
| 103 | assert!(!matches_config( |
| 104 | &[Box::new(name_value_attr("Config", "IntegerComputations"))], |
| 105 | TargetCapabilityFlags::empty() |
| 106 | )); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn floatingpointcomputations_attrs_does_not_match_empty() { |
| 111 | assert!(!matches_config( |
| 112 | &[Box::new(name_value_attr( |
| 113 | "Config", |
| 114 | "FloatingPointComputations" |
| 115 | ))], |
| 116 | TargetCapabilityFlags::empty() |
| 117 | )); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn unrestricted_attrs_does_not_match_empty() { |
| 122 | assert!(!matches_config( |
| 123 | &[Box::new(name_value_attr("Config", "Unrestricted"))], |
| 124 | TargetCapabilityFlags::empty() |
| 125 | )); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn unrestricted_attrs_matches_all() { |
| 130 | assert!(matches_config( |
| 131 | &[Box::new(name_value_attr("Config", "Unrestricted"))], |
| 132 | TargetCapabilityFlags::all() |
| 133 | )); |
| 134 | } |
| 135 | |