Skip to content

Commit

Permalink
test: capture full code generated
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Aug 25, 2024
1 parent e45e6b7 commit 9be2d97
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/tool/core/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ fn url_to_slug(url: &str) -> anyhow::Result<String> {

#[cfg(test)]
mod tests {
use cli::GenerateArgs;
use rstest::rstest;

use crate::tool::core::helpers::local_store::tests::{insta_settings, title_slugs, SlugList};

use super::*;

#[test]
Expand All @@ -167,4 +172,19 @@ mod tests {
let actual = get_slug_from_args(&url);
assert!(actual.is_err());
}

#[rstest]
fn extract_solutions_from_description(title_slugs: SlugList, insta_settings: insta::Settings) {
let args = GenerateArgs {
problem: None,
should_include_problem_number: false,
};

for title_slug in title_slugs {
insta_settings.bind(|| {
let (_, code_generated) = create_module_code(title_slug, &args).unwrap();
insta::assert_snapshot!(code_generated);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
source: src/tool/core/generate.rs
expression: code_generated
---
//! Solution for https://leetcode.com/problems/add-two-numbers
//! 2. Add Two Numbers

// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { todo!("Fill in body") }
}

// << ---------------- Code below here is only for local use ---------------- >>

pub struct Solution;
use cargo_leet::ListNode;

#[cfg(test)]
mod tests {
use super::*;
use cargo_leet::ListHead;


use rstest::rstest;

#[rstest]
#[case(ListHead::from(vec![2,4,3]).into(), ListHead::from(vec![5,6,4]).into(), ListHead::from(vec![7,0,8]).into())]
#[case(ListHead::from(vec![0]).into(), ListHead::from(vec![0]).into(), ListHead::from(vec![0]).into())]
#[case(ListHead::from(vec![9,9,9,9,9,9,9]).into(), ListHead::from(vec![9,9,9,9]).into(), ListHead::from(vec![8,9,9,9,0,0,0,1]).into())]
fn case(#[case] l1: Option<Box<ListNode>>, #[case] l2: Option<Box<ListNode>>, #[case] expected: Option<Box<ListNode>>) {
let actual = Solution::add_two_numbers(l1, l2);
assert_eq!(actual, expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: src/tool/core/generate.rs
expression: code_generated
---
//! Solution for https://leetcode.com/problems/validate-binary-search-tree
//! 98. Validate Binary Search Tree

// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { todo!("Fill in body") }
}

// << ---------------- Code below here is only for local use ---------------- >>

pub struct Solution;
use cargo_leet::TreeNode;

#[cfg(test)]
mod tests {
use super::*;
use cargo_leet::TreeRoot;


use rstest::rstest;

#[rstest]
#[case(TreeRoot::from("[2,1,3]").into(), true)]
#[case(TreeRoot::from("[5,1,4,null,null,3,6]").into(), false)]
fn case(#[case] root: Option<Rc<RefCell<TreeNode>>>, #[case] expected: bool) {
let actual = Solution::is_valid_bst(root);
assert_eq!(actual, expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: src/tool/core/generate.rs
expression: code_generated
---
//! Solution for https://leetcode.com/problems/two-sum
//! 1. Two Sum

impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { todo!("Fill in body") }
}

// << ---------------- Code below here is only for local use ---------------- >>

pub struct Solution;

#[cfg(test)]
mod tests {
use super::*;


use rstest::rstest;

#[rstest]
#[case(vec![2,7,11,15], 9, vec![0,1])]
#[case(vec![3,2,4], 6, vec![1,2])]
#[case(vec![3,3], 6, vec![0,1])]
fn case(#[case] nums: Vec<i32>, #[case] target: i32, #[case] expected: Vec<i32>) {
let actual = Solution::two_sum(nums, target);
assert_eq!(actual, expected);
}
}

0 comments on commit 9be2d97

Please sign in to comment.