#[cfg(test)]
mod tests {
    use masl_parser::parse;

    #[test]
    fn test_parse_simple() {
        let source = "namespace commerce\nschema 1\nsubject User\n";
        let res = parse(source);
        assert!(
            res.is_ok(),
            "Failed to parse simple document: {:?}",
            res.err()
        );
        let doc = res.unwrap();
        assert_eq!(doc.namespace.name, "commerce");
        assert_eq!(doc.schema_version, 1);
        assert_eq!(doc.items.len(), 1);
    }

    #[test]
    fn test_parse_commerce_example() {
        let source = r#"
            namespace commerce
            schema 1

            subject User

            subject Group {
                relation {
                    member: User | Group::member
                }
            }

            resource Organization {
                relation {
                    owner: User
                    admin: User | Group::member
                }

                grant {
                    manage => owner | admin
                }
            }
        "#;
        let res = parse(source);
        assert!(
            res.is_ok(),
            "Failed to parse commerce example: {:?}",
            res.err()
        );
    }

    #[test]
    fn test_parse_invalid_casing() {
        // Namespace must be lowercase
        let source = "namespace Commerce\nschema 1\n";
        let res = parse(source);
        assert!(res.is_err());
    }

    #[test]
    fn test_operator_precedence() {
        // Grammar matches union < intersection < difference < unary < atom
        // manage => a | b & !c - d
        // Should parse as: (a | (b & (!(c) - d)))
        let source = r#"
            namespace test
            schema 1
            resource A {
                grant {
                    manage => a | b & !c - d
                }
            }
        "#;
        let res = parse(source);
        assert!(
            res.is_ok(),
            "Failed to parse precedence example: {:?}",
            res.err()
        );
    }
}
