Skip to content

Commit

Permalink
Added rust test
Browse files Browse the repository at this point in the history
  • Loading branch information
desdic committed Feb 24, 2024
1 parent 1daafb4 commit 64a08ce
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4.24"
44 changes: 44 additions & 0 deletions tests/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use chrono::{DateTime, Utc};

struct Person {
name: String,
born: String,
}

impl Person {
fn calc_age_in_days(&self) -> i64 {
let today = Utc::now();
let borndate = DateTime::<Utc>::from_utc(
chrono::NaiveDate::parse_from_str(&self.born, "%d-%m-%Y")
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap(),
Utc,
);

today.signed_duration_since(borndate).num_days()
}

fn print(&self, f: fn(&Person) -> String) {
println!("{}", f(self));
}
}

/*
Formatting of person
*/
fn format_person(p: &Person) -> String {
let age = p.calc_age_in_days();

format!("{} is {} days old", p.name, age)
}

fn main() {
// Create Donald
let donald = Person {
name: "Donald Duck".to_string(),
born: "07-09-1934".to_string(),
};

donald.print(format_person);
}
60 changes: 60 additions & 0 deletions tests/rust_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
describe("rust", function()
local core = require("agrolens.core")
local buffers = nil
local eq = assert.equals

it("load", function()
vim.cmd.edit("tests/rust/src/main.rs")
buffers = vim.api.nvim_list_bufs()
eq(#buffers, 1)

local content = vim.api.nvim_buf_get_lines(buffers[1], 0, -1, false)

-- make sure buffer has content
eq(string.match(content[1], "DateTime"), "DateTime")

core.get_captures({ queries = { "functions" }, bufids = buffers })
end)

it("functions", function()
local entries = core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 4)
eq(entries[1].filename, "tests/rust/src/main.rs")
eq(entries[1].lnum, 9)
eq(entries[1].col, 4)

eq(entries[1].line, " fn calc_age_in_days(&self) -> i64 {")
eq(entries[2].line, " fn print(&self, f: fn(&Person) -> String) {")
eq(entries[3].line, "fn format_person(p: &Person) -> String {")
eq(entries[4].line, "fn main() {")
end)

it("callings", function()
local entries = core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 7)
eq(entries[1].filename, "tests/rust/src/main.rs")
eq(entries[1].lnum, 10)
eq(entries[1].col, 8)

eq(entries[1].line, " let today = Utc::now();")
eq(entries[2].line, ' chrono::NaiveDate::parse_from_str(&self.born, "%d-%m-%Y")')
eq(entries[3].line, " today.signed_duration_since(borndate).num_days()")
eq(entries[4].line, " let age = p.calc_age_in_days();")
eq(entries[5].line, ' name: "Donald Duck".to_string(),')
eq(entries[6].line, ' born: "07-09-1934".to_string(),')
eq(entries[7].line, " donald.print(format_person);")
end)

it("comments", function()
local entries = core.get_captures({ queries = { "comments" }, bufids = buffers })

eq(#entries, 2)
eq(entries[1].filename, "tests/rust/src/main.rs")
eq(entries[1].lnum, 27)
eq(entries[1].col, 0)
eq(entries[1].line, "/*")
eq(entries[2].line, " // Create Donald")
end)
end)

0 comments on commit 64a08ce

Please sign in to comment.